home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / ds3100.md / readline / RCS / readline.c,v < prev   
Encoding:
Text File  |  1992-07-01  |  151.2 KB  |  6,560 lines

  1. head     1.1;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.1
  10. date     92.06.17.11.47.37;  author secor;  state Exp;
  11. branches ;
  12. next     ;
  13.  
  14.  
  15. desc
  16. @@
  17.  
  18.  
  19.  
  20. 1.1
  21. log
  22. @Initial revision
  23. @
  24. text
  25. @/* readline.c -- a general facility for reading lines of input
  26.    with emacs style editing and completion. */
  27.  
  28. /* Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
  29.  
  30.    This file contains the Readline Library (the Library), a set of
  31.    routines for providing Emacs style line input to programs that ask
  32.    for it.
  33.  
  34.    The Library is free software; you can redistribute it and/or modify
  35.    it under the terms of the GNU General Public License as published by
  36.    the Free Software Foundation; either version 2 of the License, or
  37.    (at your option) any later version.
  38.  
  39.    The Library is distributed in the hope that it will be useful,
  40.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  41.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  42.    GNU General Public License for more details.
  43.  
  44.    You should have received a copy of the GNU General Public License
  45.    along with this program; if not, write to the Free Software
  46.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  47.  
  48. /* Remove these declarations when we have a complete libgnu.a. */
  49. /* #define STATIC_MALLOC */
  50. #if !defined (STATIC_MALLOC)
  51. extern char *xmalloc (), *xrealloc ();
  52. #else
  53. static char *xmalloc (), *xrealloc ();
  54. #endif /* STATIC_MALLOC */
  55.  
  56. #include "sysdep.h"
  57. #include <sys/types.h>
  58. #include <stdio.h>
  59. #include <fcntl.h>
  60. #ifndef    NO_SYS_FILE
  61. #include <sys/file.h>
  62. #endif
  63. #include <signal.h>
  64. #include <sys/dir.h>
  65.  
  66. #if defined (HAVE_UNISTD_H)
  67. #  include <unistd.h>
  68. #endif
  69.  
  70. #define NEW_TTY_DRIVER
  71. #define HAVE_BSD_SIGNALS
  72. /* #define USE_XON_XOFF */
  73.  
  74. #ifdef __MSDOS__
  75. #undef NEW_TTY_DRIVER
  76. #undef HAVE_BSD_SIGNALS
  77. #endif
  78.  
  79. /* Some USG machines have BSD signal handling (sigblock, sigsetmask, etc.) */
  80. #if defined (USG) && !defined (hpux)
  81. #undef HAVE_BSD_SIGNALS
  82. #endif
  83.  
  84. /* System V machines use termio. */
  85. #if !defined (_POSIX_VERSION)
  86. #  if defined (USG) || defined (hpux) || defined (Xenix) || defined (sgi) || defined (DGUX)
  87. #    undef NEW_TTY_DRIVER
  88. #    define TERMIO_TTY_DRIVER
  89. #    include <termio.h>
  90. #    if !defined (TCOON)
  91. #      define TCOON 1
  92. #    endif
  93. #  endif /* USG || hpux || Xenix || sgi || DUGX */
  94. #endif /* !_POSIX_VERSION */
  95.  
  96. /* Posix systems use termios and the Posix signal functions. */
  97. #if defined (_POSIX_VERSION)
  98. #  if !defined (TERMIOS_MISSING)
  99. #    undef NEW_TTY_DRIVER
  100. #    define TERMIOS_TTY_DRIVER
  101. #    include <termios.h>
  102. #  endif /* !TERMIOS_MISSING */
  103. #  define HAVE_POSIX_SIGNALS
  104. #  if !defined (O_NDELAY)
  105. #    define O_NDELAY O_NONBLOCK    /* Posix-style non-blocking i/o */
  106. #  endif /* O_NDELAY */
  107. #endif /* _POSIX_VERSION */
  108.  
  109. /* Other (BSD) machines use sgtty. */
  110. #if defined (NEW_TTY_DRIVER)
  111. #include <sgtty.h>
  112. #endif
  113.  
  114. /* Define _POSIX_VDISABLE if we are not using the `new' tty driver and
  115.    it is not already defined.  It is used both to determine if a
  116.    special character is disabled and to disable certain special
  117.    characters.  Posix systems should set to 0, USG systems to -1. */
  118. #if !defined (NEW_TTY_DRIVER) && !defined (_POSIX_VDISABLE)
  119. #  if defined (_POSIX_VERSION)
  120. #    define _POSIX_VDISABLE 0
  121. #  else /* !_POSIX_VERSION */
  122. #    define _POSIX_VDISABLE -1
  123. #  endif /* !_POSIX_VERSION */
  124. #endif /* !NEW_TTY_DRIVER && !_POSIX_VDISABLE */
  125.  
  126. #include <errno.h>
  127. extern int errno;
  128.  
  129. #include <setjmp.h>
  130. #include <sys/stat.h>
  131.  
  132. /* Posix macro to check file in statbuf for directory-ness. */
  133. #if defined (S_IFDIR) && !defined (S_ISDIR)
  134. #define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR)
  135. #endif
  136.  
  137. #ifndef __MSDOS__
  138. /* These next are for filename completion.  Perhaps this belongs
  139.    in a different place. */
  140. #include <pwd.h>
  141. #endif /* __MSDOS__ */
  142.  
  143. #if defined (USG) && !defined (isc386) && !defined (sgi)
  144. struct passwd *getpwuid (), *getpwent ();
  145. #endif
  146.  
  147. /* #define HACK_TERMCAP_MOTION */
  148.  
  149. /* Some standard library routines. */
  150. #include "readline.h"
  151. #include "history.h"
  152.  
  153. #ifndef digit
  154. #define digit(c)  ((c) >= '0' && (c) <= '9')
  155. #endif
  156.  
  157. #ifndef isletter
  158. #define isletter(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
  159. #endif
  160.  
  161. #ifndef digit_value
  162. #define digit_value(c) ((c) - '0')
  163. #endif
  164.  
  165. #ifndef member
  166. #define member(c, s) ((c) ? index ((s), (c)) : 0)
  167. #endif
  168.  
  169. #ifndef isident
  170. #define isident(c) ((isletter(c) || digit(c) || c == '_'))
  171. #endif
  172.  
  173. #ifndef exchange
  174. #define exchange(x, y) {int temp = x; x = y; y = temp;}
  175. #endif
  176.  
  177. #if !defined (rindex)
  178. extern char *rindex ();
  179. #endif /* rindex */
  180.  
  181. #if !defined (index)
  182. extern char *index ();
  183. #endif /* index */
  184.  
  185. extern char *getenv ();
  186. extern char *tilde_expand ();
  187.  
  188. static update_line ();
  189. static void output_character_function ();
  190. static delete_chars ();
  191. static insert_some_chars ();
  192.  
  193. #if defined (VOID_SIGHANDLER)
  194. #  define sighandler void
  195. #else
  196. #  define sighandler int
  197. #endif /* VOID_SIGHANDLER */
  198.  
  199. /* This typedef is equivalant to the one for Function; it allows us
  200.    to say SigHandler *foo = signal (SIGKILL, SIG_IGN); */
  201. typedef sighandler SigHandler ();
  202.  
  203. /* If on, then readline handles signals in a way that doesn't screw. */
  204. #define HANDLE_SIGNALS
  205.  
  206. #ifdef __GO32__
  207. #include <pc.h>
  208. #undef HANDLE_SIGNALS
  209. #endif
  210.  
  211.  
  212. /* **************************************************************** */
  213. /*                                    */
  214. /*            Line editing input utility            */
  215. /*                                    */
  216. /* **************************************************************** */
  217.  
  218. /* A pointer to the keymap that is currently in use.
  219.    By default, it is the standard emacs keymap. */
  220. Keymap booger;
  221. Keymap keymap = emacs_standard_keymap;
  222.  
  223. #define no_mode -1
  224. #define vi_mode 0
  225. #define emacs_mode 1
  226.  
  227. /* The current style of editing. */
  228. int rl_editing_mode = emacs_mode;
  229.  
  230. /* Non-zero if the previous command was a kill command. */
  231. static int last_command_was_kill = 0;
  232.  
  233. /* The current value of the numeric argument specified by the user. */
  234. int rl_numeric_arg = 1;
  235.  
  236. /* Non-zero if an argument was typed. */
  237. int rl_explicit_arg = 0;
  238.  
  239. /* Temporary value used while generating the argument. */
  240. int rl_arg_sign = 1;
  241.  
  242. /* Non-zero means we have been called at least once before. */
  243. static int rl_initialized = 0;
  244.  
  245. /* If non-zero, this program is running in an EMACS buffer. */
  246. static char *running_in_emacs = (char *)NULL;
  247.  
  248. /* The current offset in the current input line. */
  249. int rl_point;
  250.  
  251. /* Mark in the current input line. */
  252. int rl_mark;
  253.  
  254. /* Length of the current input line. */
  255. int rl_end;
  256.  
  257. /* Make this non-zero to return the current input_line. */
  258. int rl_done;
  259.  
  260. /* The last function executed by readline. */
  261. Function *rl_last_func = (Function *)NULL;
  262.  
  263. /* Top level environment for readline_internal (). */
  264. static jmp_buf readline_top_level;
  265.  
  266. /* The streams we interact with. */
  267. static FILE *in_stream, *out_stream;
  268.  
  269. /* The names of the streams that we do input and output to. */
  270. FILE *rl_instream = stdin, *rl_outstream = stdout;
  271.  
  272. /* Non-zero means echo characters as they are read. */
  273. int readline_echoing_p = 1;
  274.  
  275. /* Current prompt. */
  276. char *rl_prompt;
  277.  
  278. /* The number of characters read in order to type this complete command. */
  279. int rl_key_sequence_length = 0;
  280.  
  281. /* If non-zero, then this is the address of a function to call just
  282.    before readline_internal () prints the first prompt. */
  283. Function *rl_startup_hook = (Function *)NULL;
  284.  
  285. /* If non-zero, then this is the address of a function to call when
  286.    completing on a directory name.  The function is called with
  287.    the address of a string (the current directory name) as an arg. */
  288. Function *rl_symbolic_link_hook = (Function *)NULL;
  289.  
  290. /* What we use internally.  You should always refer to RL_LINE_BUFFER. */
  291. static char *the_line;
  292.  
  293. /* The character that can generate an EOF.  Really read from
  294.    the terminal driver... just defaulted here. */
  295. static int eof_char = CTRL ('D');
  296.  
  297. /* Non-zero makes this the next keystroke to read. */
  298. int rl_pending_input = 0;
  299.  
  300. /* Pointer to a useful terminal name. */
  301. char *rl_terminal_name = (char *)NULL;
  302.  
  303. /* Line buffer and maintenence. */
  304. char *rl_line_buffer = (char *)NULL;
  305. int rl_line_buffer_len = 0;
  306. #define DEFAULT_BUFFER_SIZE 256
  307.  
  308.  
  309. /* **************************************************************** */
  310. /*                                    */
  311. /*            `Forward' declarations              */
  312. /*                                    */
  313. /* **************************************************************** */
  314.  
  315. /* Non-zero means do not parse any lines other than comments and
  316.    parser directives. */
  317. static unsigned char parsing_conditionalized_out = 0;
  318.  
  319. /* Caseless strcmp (). */
  320. static int stricmp (), strnicmp ();
  321.  
  322. /* Non-zero means to save keys that we dispatch on in a kbd macro. */
  323. static int defining_kbd_macro = 0;
  324.  
  325.  
  326. /* **************************************************************** */
  327. /*                                    */
  328. /*            Top Level Functions                */
  329. /*                                    */
  330. /* **************************************************************** */
  331.  
  332. static void rl_prep_terminal (), rl_deprep_terminal ();
  333.  
  334. /* Read a line of input.  Prompt with PROMPT.  A NULL PROMPT means
  335.    none.  A return value of NULL means that EOF was encountered. */
  336. char *
  337. readline (prompt)
  338.      char *prompt;
  339. {
  340.   char *readline_internal ();
  341.   char *value;
  342.  
  343.   rl_prompt = prompt;
  344.  
  345.   /* If we are at EOF return a NULL string. */
  346.   if (rl_pending_input == EOF)
  347.     {
  348.       rl_pending_input = 0;
  349.       return ((char *)NULL);
  350.     }
  351.  
  352.   rl_initialize ();
  353.   rl_prep_terminal ();
  354.  
  355. #if defined (HANDLE_SIGNALS)
  356.   rl_set_signals ();
  357. #endif
  358.  
  359.   value = readline_internal ();
  360.   rl_deprep_terminal ();
  361.  
  362. #if defined (HANDLE_SIGNALS)
  363.   rl_clear_signals ();
  364. #endif
  365.  
  366.   return (value);
  367. }
  368.  
  369. /* Read a line of input from the global rl_instream, doing output on
  370.    the global rl_outstream.
  371.    If rl_prompt is non-null, then that is our prompt. */
  372. char *
  373. readline_internal ()
  374. {
  375.   int lastc, c, eof_found;
  376.  
  377.   in_stream  = rl_instream;
  378.   out_stream = rl_outstream;
  379.  
  380.   lastc = -1;
  381.   eof_found = 0;
  382.  
  383.   if (rl_startup_hook)
  384.     (*rl_startup_hook) ();
  385.  
  386.   if (!readline_echoing_p)
  387.     {
  388.       if (rl_prompt)
  389.     {
  390.       fprintf (out_stream, "%s", rl_prompt);
  391.       fflush (out_stream);
  392.     }
  393.     }
  394.   else
  395.     {
  396.       rl_on_new_line ();
  397.       rl_redisplay ();
  398. #if defined (VI_MODE)
  399.       if (rl_editing_mode == vi_mode)
  400.     rl_vi_insertion_mode ();
  401. #endif /* VI_MODE */
  402.     }
  403.  
  404.   while (!rl_done)
  405.     {
  406.       int lk = last_command_was_kill;
  407.       int code = setjmp (readline_top_level);
  408.  
  409.       if (code)
  410.     rl_redisplay ();
  411.  
  412.       if (!rl_pending_input)
  413.     {
  414.       /* Then initialize the argument and number of keys read. */
  415.       rl_init_argument ();
  416.       rl_key_sequence_length = 0;
  417.     }
  418.  
  419.       c = rl_read_key ();
  420.  
  421.       /* EOF typed to a non-blank line is a <NL>. */
  422.       if (c == EOF && rl_end)
  423.     c = NEWLINE;
  424.  
  425.       /* The character eof_char typed to blank line, and not as the
  426.      previous character is interpreted as EOF. */
  427.       if (((c == eof_char && lastc != c) || c == EOF) && !rl_end)
  428.     {
  429.       eof_found = 1;
  430.       break;
  431.     }
  432.  
  433.       lastc = c;
  434.       rl_dispatch (c, keymap);
  435.  
  436.       /* If there was no change in last_command_was_kill, then no kill
  437.      has taken place.  Note that if input is pending we are reading
  438.      a prefix command, so nothing has changed yet. */
  439.       if (!rl_pending_input)
  440.     {
  441.       if (lk == last_command_was_kill)
  442.         last_command_was_kill = 0;
  443.     }
  444.  
  445. #if defined (VI_MODE)
  446.       /* In vi mode, when you exit insert mode, the cursor moves back
  447.      over the previous character.  We explicitly check for that here. */
  448.       if (rl_editing_mode == vi_mode && keymap == vi_movement_keymap)
  449.     rl_vi_check ();
  450. #endif /* VI_MODE */
  451.  
  452.       if (!rl_done)
  453.     rl_redisplay ();
  454.     }
  455.  
  456.   /* Restore the original of this history line, iff the line that we
  457.      are editing was originally in the history, AND the line has changed. */
  458.   {
  459.     HIST_ENTRY *entry = current_history ();
  460.  
  461.     if (entry && rl_undo_list)
  462.       {
  463.     char *temp = savestring (the_line);
  464.     rl_revert_line ();
  465.     entry = replace_history_entry (where_history (), the_line,
  466.                        (HIST_ENTRY *)NULL);
  467.     free_history_entry (entry);
  468.  
  469.     strcpy (the_line, temp);
  470.     free (temp);
  471.       }
  472.   }
  473.  
  474.   /* At any rate, it is highly likely that this line has an undo list.  Get
  475.      rid of it now. */
  476.   if (rl_undo_list)
  477.     free_undo_list ();
  478.  
  479.   if (eof_found)
  480.     return (char *)NULL;
  481.   else
  482.     return (savestring (the_line));
  483. }
  484.  
  485.  
  486. /* **************************************************************** */
  487. /*                                        */
  488. /*               Signal Handling                          */
  489. /*                                    */
  490. /* **************************************************************** */
  491.  
  492. #if defined (SIGWINCH)
  493. static SigHandler *old_sigwinch = (SigHandler *)NULL;
  494.  
  495. static sighandler
  496. rl_handle_sigwinch (sig)
  497.      int sig;
  498. {
  499.   char *term;
  500.  
  501.   term = rl_terminal_name;
  502.  
  503.   if (readline_echoing_p)
  504.     {
  505.       if (!term)
  506.     term = getenv ("TERM");
  507.       if (!term)
  508.     term = "dumb";
  509.       rl_reset_terminal (term);
  510. #if defined (NOTDEF)
  511.       crlf ();
  512.       rl_forced_update_display ();
  513. #endif /* NOTDEF */
  514.     }
  515.  
  516.   if (old_sigwinch &&
  517.       old_sigwinch != (SigHandler *)SIG_IGN &&
  518.       old_sigwinch != (SigHandler *)SIG_DFL)
  519.     (*old_sigwinch) (sig);
  520. #if !defined (VOID_SIGHANDLER)
  521.   return (0);
  522. #endif /* VOID_SIGHANDLER */
  523. }
  524. #endif  /* SIGWINCH */
  525.  
  526. #if defined (HANDLE_SIGNALS)
  527. /* Interrupt handling. */
  528. static SigHandler
  529.   *old_int  = (SigHandler *)NULL,
  530.   *old_tstp = (SigHandler *)NULL,
  531.   *old_ttou = (SigHandler *)NULL,
  532.   *old_ttin = (SigHandler *)NULL,
  533.   *old_cont = (SigHandler *)NULL,
  534.   *old_alrm = (SigHandler *)NULL;
  535.  
  536. /* Handle an interrupt character. */
  537. static sighandler
  538. rl_signal_handler (sig)
  539.      int sig;
  540. {
  541. #if !defined (HAVE_BSD_SIGNALS)
  542.   /* Since the signal will not be blocked while we are in the signal
  543.      handler, ignore it until rl_clear_signals resets the catcher. */
  544.   if (sig == SIGINT)
  545.     signal (sig, SIG_IGN);
  546. #endif /* !HAVE_BSD_SIGNALS */
  547.  
  548.   switch (sig)
  549.     {
  550.     case SIGINT:
  551.       free_undo_list ();
  552.       rl_clear_message ();
  553.       rl_init_argument ();
  554.  
  555. #if defined (SIGTSTP)
  556.     case SIGTSTP:
  557.     case SIGTTOU:
  558.     case SIGTTIN:
  559. #endif /* SIGTSTP */
  560.     case SIGALRM:
  561.       rl_clean_up_for_exit ();
  562.       rl_deprep_terminal ();
  563.       rl_clear_signals ();
  564.       rl_pending_input = 0;
  565.  
  566.       kill (getpid (), sig);
  567.  
  568. #if defined (HAVE_POSIX_SIGNALS)
  569.       {
  570.     sigset_t set;
  571.  
  572.     sigemptyset (&set);
  573.     sigprocmask (SIG_SETMASK, &set, (sigset_t *)NULL);
  574.       }
  575. #else
  576. #if defined (HAVE_BSD_SIGNALS)
  577.       sigsetmask (0);
  578. #endif /* HAVE_BSD_SIGNALS */
  579. #endif /* HAVE_POSIX_SIGNALS */
  580.  
  581.       rl_prep_terminal ();
  582.       rl_set_signals ();
  583.     }
  584.  
  585. #if !defined (VOID_SIGHANDLER)
  586.   return (0);
  587. #endif /* !VOID_SIGHANDLER */
  588. }
  589.  
  590. rl_set_signals ()
  591. {
  592.   old_int = (SigHandler *)signal (SIGINT, rl_signal_handler);
  593.   if (old_int == (SigHandler *)SIG_IGN)
  594.     signal (SIGINT, SIG_IGN);
  595.  
  596.   old_alrm = (SigHandler *)signal (SIGALRM, rl_signal_handler);
  597.   if (old_alrm == (SigHandler *)SIG_IGN)
  598.     signal (SIGALRM, SIG_IGN);
  599.  
  600. #if defined (SIGTSTP)
  601.   old_tstp = (SigHandler *)signal (SIGTSTP, rl_signal_handler);
  602.   if (old_tstp == (SigHandler *)SIG_IGN)
  603.     signal (SIGTSTP, SIG_IGN);
  604. #endif
  605. #if defined (SIGTTOU)
  606.   old_ttou = (SigHandler *)signal (SIGTTOU, rl_signal_handler);
  607.   old_ttin = (SigHandler *)signal (SIGTTIN, rl_signal_handler);
  608.  
  609.   if (old_tstp == (SigHandler *)SIG_IGN)
  610.     {
  611.       signal (SIGTTOU, SIG_IGN);
  612.       signal (SIGTTIN, SIG_IGN);
  613.     }
  614. #endif
  615.  
  616. #if defined (SIGWINCH)
  617.   old_sigwinch = (SigHandler *)signal (SIGWINCH, rl_handle_sigwinch);
  618. #endif
  619. }
  620.  
  621. rl_clear_signals ()
  622. {
  623.   signal (SIGINT, old_int);
  624.   signal (SIGALRM, old_alrm);
  625.  
  626. #if defined (SIGTSTP)
  627.   signal (SIGTSTP, old_tstp);
  628. #endif
  629.  
  630. #if defined (SIGTTOU)
  631.   signal (SIGTTOU, old_ttou);
  632.   signal (SIGTTIN, old_ttin);
  633. #endif
  634.  
  635. #if defined (SIGWINCH)
  636.       signal (SIGWINCH, old_sigwinch);
  637. #endif
  638. }
  639. #endif  /* HANDLE_SIGNALS */
  640.  
  641.  
  642. /* **************************************************************** */
  643. /*                                    */
  644. /*            Character Input Buffering               */
  645. /*                                    */
  646. /* **************************************************************** */
  647.  
  648. #if defined (USE_XON_XOFF)
  649. /* If the terminal was in xoff state when we got to it, then xon_char
  650.    contains the character that is supposed to start it again. */
  651. static int xon_char, xoff_state;
  652. #endif /* USE_XON_XOFF */
  653.  
  654. static int pop_index = 0, push_index = 0, ibuffer_len = 511;
  655. static unsigned char ibuffer[512];
  656.  
  657. /* Non-null means it is a pointer to a function to run while waiting for
  658.    character input. */
  659. Function *rl_event_hook = (Function *)NULL;
  660.  
  661. #define any_typein (push_index != pop_index)
  662.  
  663. /* Add KEY to the buffer of characters to be read. */
  664. rl_stuff_char (key)
  665.      int key;
  666. {
  667.   if (key == EOF)
  668.     {
  669.       key = NEWLINE;
  670.       rl_pending_input = EOF;
  671.     }
  672.   ibuffer[push_index++] = key;
  673.   if (push_index >= ibuffer_len)
  674.     push_index = 0;
  675. }
  676.  
  677. /* Return the amount of space available in the
  678.    buffer for stuffing characters. */
  679. int
  680. ibuffer_space ()
  681. {
  682.   if (pop_index > push_index)
  683.     return (pop_index - push_index);
  684.   else
  685.     return (ibuffer_len - (push_index - pop_index));
  686. }
  687.  
  688. /* Get a key from the buffer of characters to be read.
  689.    Return the key in KEY.
  690.    Result is KEY if there was a key, or 0 if there wasn't. */
  691. int
  692. rl_get_char (key)
  693.      int *key;
  694. {
  695.   if (push_index == pop_index)
  696.     return (0);
  697.  
  698.   *key = ibuffer[pop_index++];
  699.  
  700.   if (pop_index >= ibuffer_len)
  701.     pop_index = 0;
  702.  
  703.   return (1);
  704. }
  705.  
  706. /* Stuff KEY into the *front* of the input buffer.
  707.    Returns non-zero if successful, zero if there is
  708.    no space left in the buffer. */
  709. int
  710. rl_unget_char (key)
  711.      int key;
  712. {
  713.   if (ibuffer_space ())
  714.     {
  715.       pop_index--;
  716.       if (pop_index < 0)
  717.     pop_index = ibuffer_len - 1;
  718.       ibuffer[pop_index] = key;
  719.       return (1);
  720.     }
  721.   return (0);
  722. }
  723.  
  724. /* If a character is available to be read, then read it
  725.    and stuff it into IBUFFER.  Otherwise, just return. */
  726. rl_gather_tyi ()
  727. {
  728. #ifdef __GO32__
  729.   char input;
  730.   if (isatty(0))
  731.   {
  732.     int i = rl_getc();
  733.     if (i != EOF)
  734.       rl_stuff_char(i);
  735.   }
  736.   else
  737.     if (kbhit() && ibuffer_space())
  738.       rl_stuff_char(getkey());
  739. #else
  740.   int tty = fileno (in_stream);
  741.   register int tem, result = -1;
  742.   long chars_avail;
  743.   char input;
  744.  
  745. #if defined (FIONREAD)
  746.   result = ioctl (tty, FIONREAD, &chars_avail);
  747. #endif
  748.  
  749.   if (result == -1)
  750.     {
  751.       int flags;
  752.  
  753.       flags = fcntl (tty, F_GETFL, 0);
  754.  
  755.       fcntl (tty, F_SETFL, (flags | O_NDELAY));
  756.       chars_avail = read (tty, &input, 1);
  757.  
  758.       fcntl (tty, F_SETFL, flags);
  759.       if (chars_avail == -1 && errno == EAGAIN)
  760.     return;
  761.     }
  762.  
  763.   /* If there's nothing available, don't waste time trying to read
  764.      something. */
  765.   if (chars_avail == 0)
  766.     return;
  767.  
  768.   tem = ibuffer_space ();
  769.  
  770.   if (chars_avail > tem)
  771.     chars_avail = tem;
  772.  
  773.   /* One cannot read all of the available input.  I can only read a single
  774.      character at a time, or else programs which require input can be
  775.      thwarted.  If the buffer is larger than one character, I lose.
  776.      Damn! */
  777.   if (tem < ibuffer_len)
  778.     chars_avail = 0;
  779.  
  780.   if (result != -1)
  781.     {
  782.       while (chars_avail--)
  783.     rl_stuff_char (rl_getc (in_stream));
  784.     }
  785.   else
  786.     {
  787.       if (chars_avail)
  788.     rl_stuff_char (input);
  789.     }
  790. #endif /* def __GO32__/else */
  791. }
  792.  
  793. static int next_macro_key ();
  794. /* Read a key, including pending input. */
  795. int
  796. rl_read_key ()
  797. {
  798.   int c;
  799.  
  800.   rl_key_sequence_length++;
  801.  
  802.   if (rl_pending_input)
  803.     {
  804.       c = rl_pending_input;
  805.       rl_pending_input = 0;
  806.     }
  807.   else
  808.     {
  809.       /* If input is coming from a macro, then use that. */
  810.       if (c = next_macro_key ())
  811.     return (c);
  812.  
  813.       /* If the user has an event function, then call it periodically. */
  814.       if (rl_event_hook)
  815.     {
  816.       while (rl_event_hook && !rl_get_char (&c))
  817.         {
  818.           (*rl_event_hook) ();
  819.           rl_gather_tyi ();
  820.         }
  821.     }
  822.       else
  823.     {
  824.       if (!rl_get_char (&c))
  825.         c = rl_getc (in_stream);
  826.     }
  827.     }
  828.  
  829.   return (c);
  830. }
  831.  
  832. /* I'm beginning to hate the declaration rules for various compilers. */
  833. static void add_macro_char (), with_macro_input ();
  834.  
  835. /* Do the command associated with KEY in MAP.
  836.    If the associated command is really a keymap, then read
  837.    another key, and dispatch into that map. */
  838. rl_dispatch (key, map)
  839.      register int key;
  840.      Keymap map;
  841. {
  842.  
  843.   if (defining_kbd_macro)
  844.     add_macro_char (key);
  845.  
  846.   if (key > 127 && key < 256)
  847.     {
  848.       if (map[ESC].type == ISKMAP)
  849.     {
  850.       map = (Keymap)map[ESC].function;
  851.       key -= 128;
  852.       rl_dispatch (key, map);
  853.     }
  854.       else
  855.     ding ();
  856.       return;
  857.     }
  858.  
  859.   switch (map[key].type)
  860.     {
  861.     case ISFUNC:
  862.       {
  863.     Function *func = map[key].function;
  864.  
  865.     if (func != (Function *)NULL)
  866.       {
  867.         /* Special case rl_do_lowercase_version (). */
  868.         if (func == rl_do_lowercase_version)
  869.           {
  870.         rl_dispatch (to_lower (key), map);
  871.         return;
  872.           }
  873.  
  874.         (*map[key].function)(rl_numeric_arg * rl_arg_sign, key);
  875.  
  876.         /* If we have input pending, then the last command was a prefix
  877.            command.  Don't change the state of rl_last_func.  Otherwise,
  878.            remember the last command executed in this variable. */
  879.         if (!rl_pending_input)
  880.           rl_last_func = map[key].function;
  881.       }
  882.     else
  883.       {
  884.         rl_abort ();
  885.         return;
  886.       }
  887.       }
  888.       break;
  889.  
  890.     case ISKMAP:
  891.       if (map[key].function != (Function *)NULL)
  892.     {
  893.       int newkey;
  894.  
  895.       rl_key_sequence_length++;
  896.       newkey = rl_read_key ();
  897.       rl_dispatch (newkey, (Keymap)map[key].function);
  898.     }
  899.       else
  900.     {
  901.       rl_abort ();
  902.       return;
  903.     }
  904.       break;
  905.  
  906.     case ISMACR:
  907.       if (map[key].function != (Function *)NULL)
  908.     {
  909.       char *macro;
  910.  
  911.       macro = savestring ((char *)map[key].function);
  912.       with_macro_input (macro);
  913.       return;
  914.     }
  915.       break;
  916.     }
  917. }
  918.  
  919.  
  920. /* **************************************************************** */
  921. /*                                    */
  922. /*            Hacking Keyboard Macros             */
  923. /*                                    */
  924. /* **************************************************************** */
  925.  
  926. /* The currently executing macro string.  If this is non-zero,
  927.    then it is a malloc ()'ed string where input is coming from. */
  928. static char *executing_macro = (char *)NULL;
  929.  
  930. /* The offset in the above string to the next character to be read. */
  931. static int executing_macro_index = 0;
  932.  
  933. /* The current macro string being built.  Characters get stuffed
  934.    in here by add_macro_char (). */
  935. static char *current_macro = (char *)NULL;
  936.  
  937. /* The size of the buffer allocated to current_macro. */
  938. static int current_macro_size = 0;
  939.  
  940. /* The index at which characters are being added to current_macro. */
  941. static int current_macro_index = 0;
  942.  
  943. /* A structure used to save nested macro strings.
  944.    It is a linked list of string/index for each saved macro. */
  945. struct saved_macro {
  946.   struct saved_macro *next;
  947.   char *string;
  948.   int index;
  949. };
  950.  
  951. /* The list of saved macros. */
  952. struct saved_macro *macro_list = (struct saved_macro *)NULL;
  953.  
  954. /* Forward declarations of static functions.  Thank you C. */
  955. static void push_executing_macro (), pop_executing_macro ();
  956.  
  957. /* This one has to be declared earlier in the file. */
  958. /* static void add_macro_char (); */
  959.  
  960. /* Set up to read subsequent input from STRING.
  961.    STRING is free ()'ed when we are done with it. */
  962. static void
  963. with_macro_input (string)
  964.      char *string;
  965. {
  966.   push_executing_macro ();
  967.   executing_macro = string;
  968.   executing_macro_index = 0;
  969. }
  970.  
  971. /* Return the next character available from a macro, or 0 if
  972.    there are no macro characters. */
  973. static int
  974. next_macro_key ()
  975. {
  976.   if (!executing_macro)
  977.     return (0);
  978.  
  979.   if (!executing_macro[executing_macro_index])
  980.     {
  981.       pop_executing_macro ();
  982.       return (next_macro_key ());
  983.     }
  984.  
  985.   return (executing_macro[executing_macro_index++]);
  986. }
  987.  
  988. /* Save the currently executing macro on a stack of saved macros. */
  989. static void
  990. push_executing_macro ()
  991. {
  992.   struct saved_macro *saver;
  993.  
  994.   saver = (struct saved_macro *)xmalloc (sizeof (struct saved_macro));
  995.   saver->next = macro_list;
  996.   saver->index = executing_macro_index;
  997.   saver->string = executing_macro;
  998.  
  999.   macro_list = saver;
  1000. }
  1001.  
  1002. /* Discard the current macro, replacing it with the one
  1003.    on the top of the stack of saved macros. */
  1004. static void
  1005. pop_executing_macro ()
  1006. {
  1007.   if (executing_macro)
  1008.     free (executing_macro);
  1009.  
  1010.   executing_macro = (char *)NULL;
  1011.   executing_macro_index = 0;
  1012.  
  1013.   if (macro_list)
  1014.     {
  1015.       struct saved_macro *disposer = macro_list;
  1016.       executing_macro = macro_list->string;
  1017.       executing_macro_index = macro_list->index;
  1018.       macro_list = macro_list->next;
  1019.       free (disposer);
  1020.     }
  1021. }
  1022.  
  1023. /* Add a character to the macro being built. */
  1024. static void
  1025. add_macro_char (c)
  1026.      int c;
  1027. {
  1028.   if (current_macro_index + 1 >= current_macro_size)
  1029.     {
  1030.       if (!current_macro)
  1031.     current_macro = (char *)xmalloc (current_macro_size = 25);
  1032.       else
  1033.     current_macro =
  1034.       (char *)xrealloc (current_macro, current_macro_size += 25);
  1035.     }
  1036.  
  1037.   current_macro[current_macro_index++] = c;
  1038.   current_macro[current_macro_index] = '\0';
  1039. }
  1040.  
  1041. /* Begin defining a keyboard macro.
  1042.    Keystrokes are recorded as they are executed.
  1043.    End the definition with rl_end_kbd_macro ().
  1044.    If a numeric argument was explicitly typed, then append this
  1045.    definition to the end of the existing macro, and start by
  1046.    re-executing the existing macro. */
  1047. rl_start_kbd_macro (ignore1, ignore2)
  1048.      int ignore1, ignore2;
  1049. {
  1050.   if (defining_kbd_macro)
  1051.     rl_abort ();
  1052.  
  1053.   if (rl_explicit_arg)
  1054.     {
  1055.       if (current_macro)
  1056.     with_macro_input (savestring (current_macro));
  1057.     }
  1058.   else
  1059.     current_macro_index = 0;
  1060.  
  1061.   defining_kbd_macro = 1;
  1062. }
  1063.  
  1064. /* Stop defining a keyboard macro.
  1065.    A numeric argument says to execute the macro right now,
  1066.    that many times, counting the definition as the first time. */
  1067. rl_end_kbd_macro (count, ignore)
  1068.      int count, ignore;
  1069. {
  1070.   if (!defining_kbd_macro)
  1071.     rl_abort ();
  1072.  
  1073.   current_macro_index -= (rl_key_sequence_length - 1);
  1074.   current_macro[current_macro_index] = '\0';
  1075.  
  1076.   defining_kbd_macro = 0;
  1077.  
  1078.   rl_call_last_kbd_macro (--count, 0);
  1079. }
  1080.  
  1081. /* Execute the most recently defined keyboard macro.
  1082.    COUNT says how many times to execute it. */
  1083. rl_call_last_kbd_macro (count, ignore)
  1084.      int count, ignore;
  1085. {
  1086.   if (!current_macro)
  1087.     rl_abort ();
  1088.  
  1089.   while (count--)
  1090.     with_macro_input (savestring (current_macro));
  1091. }
  1092.  
  1093.  
  1094. /* **************************************************************** */
  1095. /*                                    */
  1096. /*            Initializations                 */
  1097. /*                                    */
  1098. /* **************************************************************** */
  1099.  
  1100. /* Initliaze readline (and terminal if not already). */
  1101. rl_initialize ()
  1102. {
  1103.   extern char *rl_display_prompt;
  1104.  
  1105.   /* If we have never been called before, initialize the
  1106.      terminal and data structures. */
  1107.   if (!rl_initialized)
  1108.     {
  1109.       readline_initialize_everything ();
  1110.       rl_initialized++;
  1111.     }
  1112.  
  1113.   /* Initalize the current line information. */
  1114.   rl_point = rl_end = 0;
  1115.   the_line = rl_line_buffer;
  1116.   the_line[0] = 0;
  1117.  
  1118.   /* We aren't done yet.  We haven't even gotten started yet! */
  1119.   rl_done = 0;
  1120.  
  1121.   /* Tell the history routines what is going on. */
  1122.   start_using_history ();
  1123.  
  1124.   /* Make the display buffer match the state of the line. */
  1125.   {
  1126.     extern char *rl_display_prompt;
  1127.     extern int forced_display;
  1128.  
  1129.     rl_on_new_line ();
  1130.  
  1131.     rl_display_prompt = rl_prompt ? rl_prompt : "";
  1132.     forced_display = 1;
  1133.   }
  1134.  
  1135.   /* No such function typed yet. */
  1136.   rl_last_func = (Function *)NULL;
  1137.  
  1138.   /* Parsing of key-bindings begins in an enabled state. */
  1139.   parsing_conditionalized_out = 0;
  1140. }
  1141.  
  1142. /* Initialize the entire state of the world. */
  1143. readline_initialize_everything ()
  1144. {
  1145.   /* Find out if we are running in Emacs. */
  1146.   running_in_emacs = getenv ("EMACS");
  1147.  
  1148.   /* Allocate data structures. */
  1149.   if (!rl_line_buffer)
  1150.     rl_line_buffer =
  1151.       (char *)xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
  1152.  
  1153.   /* Initialize the terminal interface. */
  1154.   init_terminal_io ((char *)NULL);
  1155.  
  1156.   /* Bind tty characters to readline functions. */
  1157.   readline_default_bindings ();
  1158.  
  1159.   /* Initialize the function names. */
  1160.   rl_initialize_funmap ();
  1161.  
  1162.   /* Read in the init file. */
  1163.   rl_read_init_file ((char *)NULL);
  1164.  
  1165.   /* If the completion parser's default word break characters haven't
  1166.      been set yet, then do so now. */
  1167.   {
  1168.     extern char *rl_completer_word_break_characters;
  1169.     extern char *rl_basic_word_break_characters;
  1170.  
  1171.     if (rl_completer_word_break_characters == (char *)NULL)
  1172.       rl_completer_word_break_characters = rl_basic_word_break_characters;
  1173.   }
  1174. }
  1175.  
  1176. /* If this system allows us to look at the values of the regular
  1177.    input editing characters, then bind them to their readline
  1178.    equivalents, iff the characters are not bound to keymaps. */
  1179. readline_default_bindings ()
  1180. {
  1181. #ifndef __GO32__
  1182.  
  1183. #if defined (NEW_TTY_DRIVER)
  1184.   struct sgttyb ttybuff;
  1185.   int tty = fileno (rl_instream);
  1186.  
  1187.   if (ioctl (tty, TIOCGETP, &ttybuff) != -1)
  1188.     {
  1189.       int erase, kill;
  1190.  
  1191.       erase = ttybuff.sg_erase;
  1192.       kill  = ttybuff.sg_kill;
  1193.  
  1194.       if (erase != -1 && keymap[erase].type == ISFUNC)
  1195.     keymap[erase].function = rl_rubout;
  1196.  
  1197.       if (kill != -1 && keymap[kill].type == ISFUNC)
  1198.     keymap[kill].function = rl_unix_line_discard;
  1199.     }
  1200.  
  1201. #if defined (TIOCGLTC)
  1202.   {
  1203.     struct ltchars lt;
  1204.  
  1205.     if (ioctl (tty, TIOCGLTC, <) != -1)
  1206.       {
  1207.     int erase, nextc;
  1208.  
  1209.     erase = lt.t_werasc;
  1210.     nextc = lt.t_lnextc;
  1211.  
  1212.     if (erase != -1 && keymap[erase].type == ISFUNC)
  1213.       keymap[erase].function = rl_unix_word_rubout;
  1214.  
  1215.     if (nextc != -1 && keymap[nextc].type == ISFUNC)
  1216.       keymap[nextc].function = rl_quoted_insert;
  1217.       }
  1218.   }
  1219. #endif /* TIOCGLTC */
  1220. #else /* not NEW_TTY_DRIVER */
  1221.  
  1222. #if defined (TERMIOS_TTY_DRIVER)
  1223.   struct termios ttybuff;
  1224. #else
  1225.   struct termio ttybuff;
  1226. #endif /* TERMIOS_TTY_DRIVER */
  1227.   int tty = fileno (rl_instream);
  1228.  
  1229. #if defined (TERMIOS_TTY_DRIVER)
  1230.   if (tcgetattr (tty, &ttybuff) != -1)
  1231. #else
  1232.   if (ioctl (tty, TCGETA, &ttybuff) != -1)
  1233. #endif /* !TERMIOS_TTY_DRIVER */
  1234.     {
  1235.       int erase, kill;
  1236.  
  1237.       erase = ttybuff.c_cc[VERASE];
  1238.       kill = ttybuff.c_cc[VKILL];
  1239.  
  1240.       if (erase != _POSIX_VDISABLE &&
  1241.       keymap[(unsigned char)erase].type == ISFUNC)
  1242.     keymap[(unsigned char)erase].function = rl_rubout;
  1243.  
  1244.       if (kill != _POSIX_VDISABLE &&
  1245.       keymap[(unsigned char)kill].type == ISFUNC)
  1246.     keymap[(unsigned char)kill].function = rl_unix_line_discard;
  1247.  
  1248. #if defined (VLNEXT) && defined (TERMIOS_TTY_DRIVER)
  1249.       {
  1250.     int nextc;
  1251.  
  1252.     nextc = ttybuff.c_cc[VLNEXT];
  1253.  
  1254.     if (nextc != _POSIX_VDISABLE &&
  1255.         keymap[(unsigned char)nextc].type == ISFUNC)
  1256.       keymap[(unsigned char)nextc].function = rl_quoted_insert;
  1257.       }
  1258. #endif /* VLNEXT && TERMIOS_TTY_DRIVER */
  1259.  
  1260. #if defined (VWERASE)
  1261.       {
  1262.     int werase;
  1263.  
  1264.     werase = ttybuff.c_cc[VWERASE];
  1265.  
  1266.     if (werase != _POSIX_VDISABLE &&
  1267.         keymap[(unsigned char)werase].type == ISFUNC)
  1268.       keymap[(unsigned char)werase].function = rl_unix_word_rubout;
  1269.       }
  1270. #endif /* VWERASE */
  1271.     }
  1272. #endif /* !NEW_TTY_DRIVER */
  1273. #endif /* def __GO32__ */
  1274. }
  1275.  
  1276.  
  1277. /* **************************************************************** */
  1278. /*                                    */
  1279. /*            Numeric Arguments                */
  1280. /*                                    */
  1281. /* **************************************************************** */
  1282.  
  1283. /* Handle C-u style numeric args, as well as M--, and M-digits. */
  1284.  
  1285. /* Add the current digit to the argument in progress. */
  1286. rl_digit_argument (ignore, key)
  1287.      int ignore, key;
  1288. {
  1289.   rl_pending_input = key;
  1290.   rl_digit_loop ();
  1291. }
  1292.  
  1293. /* What to do when you abort reading an argument. */
  1294. rl_discard_argument ()
  1295. {
  1296.   ding ();
  1297.   rl_clear_message ();
  1298.   rl_init_argument ();
  1299. }
  1300.  
  1301. /* Create a default argument. */
  1302. rl_init_argument ()
  1303. {
  1304.   rl_numeric_arg = rl_arg_sign = 1;
  1305.   rl_explicit_arg = 0;
  1306. }
  1307.  
  1308. /* C-u, universal argument.  Multiply the current argument by 4.
  1309.    Read a key.  If the key has nothing to do with arguments, then
  1310.    dispatch on it.  If the key is the abort character then abort. */
  1311. rl_universal_argument ()
  1312. {
  1313.   rl_numeric_arg *= 4;
  1314.   rl_digit_loop ();
  1315. }
  1316.  
  1317. rl_digit_loop ()
  1318. {
  1319.   int key, c;
  1320.   while (1)
  1321.     {
  1322.       rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg);
  1323.       key = c = rl_read_key ();
  1324.  
  1325.       if (keymap[c].type == ISFUNC &&
  1326.       keymap[c].function == rl_universal_argument)
  1327.     {
  1328.       rl_numeric_arg *= 4;
  1329.       continue;
  1330.     }
  1331.       c = UNMETA (c);
  1332.       if (numeric (c))
  1333.     {
  1334.       if (rl_explicit_arg)
  1335.         rl_numeric_arg = (rl_numeric_arg * 10) + (c - '0');
  1336.       else
  1337.         rl_numeric_arg = (c - '0');
  1338.       rl_explicit_arg = 1;
  1339.     }
  1340.       else
  1341.     {
  1342.       if (c == '-' && !rl_explicit_arg)
  1343.         {
  1344.           rl_numeric_arg = 1;
  1345.           rl_arg_sign = -1;
  1346.         }
  1347.       else
  1348.         {
  1349.           rl_clear_message ();
  1350.           rl_dispatch (key, keymap);
  1351.           return;
  1352.         }
  1353.     }
  1354.     }
  1355. }
  1356.  
  1357.  
  1358. /* **************************************************************** */
  1359. /*                                    */
  1360. /*            Display stuff                    */
  1361. /*                                    */
  1362. /* **************************************************************** */
  1363.  
  1364. /* This is the stuff that is hard for me.  I never seem to write good
  1365.    display routines in C.  Let's see how I do this time. */
  1366.  
  1367. /* (PWP) Well... Good for a simple line updater, but totally ignores
  1368.    the problems of input lines longer than the screen width.
  1369.  
  1370.    update_line and the code that calls it makes a multiple line,
  1371.    automatically wrapping line update.  Carefull attention needs
  1372.    to be paid to the vertical position variables.
  1373.  
  1374.    handling of terminals with autowrap on (incl. DEC braindamage)
  1375.    could be improved a bit.  Right now I just cheat and decrement
  1376.    screenwidth by one. */
  1377.  
  1378. /* Keep two buffers; one which reflects the current contents of the
  1379.    screen, and the other to draw what we think the new contents should
  1380.    be.  Then compare the buffers, and make whatever changes to the
  1381.    screen itself that we should.  Finally, make the buffer that we
  1382.    just drew into be the one which reflects the current contents of the
  1383.    screen, and place the cursor where it belongs.
  1384.  
  1385.    Commands that want to can fix the display themselves, and then let
  1386.    this function know that the display has been fixed by setting the
  1387.    RL_DISPLAY_FIXED variable.  This is good for efficiency. */
  1388.  
  1389. /* Termcap variables: */
  1390. extern char *term_up, *term_dc, *term_cr;
  1391. extern int screenheight, screenwidth, terminal_can_insert;
  1392.  
  1393. /* What YOU turn on when you have handled all redisplay yourself. */
  1394. int rl_display_fixed = 0;
  1395.  
  1396. /* The visible cursor position.  If you print some text, adjust this. */
  1397. int last_c_pos = 0;
  1398. int last_v_pos = 0;
  1399.  
  1400. /* The last left edge of text that was displayed.  This is used when
  1401.    doing horizontal scrolling.  It shifts in thirds of a screenwidth. */
  1402. static int last_lmargin = 0;
  1403.  
  1404. /* The line display buffers.  One is the line currently displayed on
  1405.    the screen.  The other is the line about to be displayed. */
  1406. static char *visible_line = (char *)NULL;
  1407. static char *invisible_line = (char *)NULL;
  1408.  
  1409. /* Number of lines currently on screen minus 1. */
  1410. int vis_botlin = 0;
  1411.  
  1412. /* A buffer for `modeline' messages. */
  1413. char msg_buf[128];
  1414.  
  1415. /* Non-zero forces the redisplay even if we thought it was unnecessary. */
  1416. int forced_display = 0;
  1417.  
  1418. /* The stuff that gets printed out before the actual text of the line.
  1419.    This is usually pointing to rl_prompt. */
  1420. char *rl_display_prompt = (char *)NULL;
  1421.  
  1422. /* Default and initial buffer size.  Can grow. */
  1423. static int line_size = 1024;
  1424.  
  1425. /* Non-zero means to always use horizontal scrolling in line display. */
  1426. static int horizontal_scroll_mode = 0;
  1427.  
  1428. /* Non-zero means to display an asterisk at the starts of history lines
  1429.    which have been modified. */
  1430. static int mark_modified_lines = 0;
  1431.  
  1432. /* Non-zero means to use a visible bell if one is available rather than
  1433.    simply ringing the terminal bell. */
  1434. static int prefer_visible_bell = 0;
  1435.  
  1436. /* I really disagree with this, but my boss (among others) insists that we
  1437.    support compilers that don't work.  I don't think we are gaining by doing
  1438.    so; what is the advantage in producing better code if we can't use it? */
  1439. /* The following two declarations belong inside the
  1440.    function block, not here. */
  1441. static void move_cursor_relative ();
  1442. static void output_some_chars ();
  1443. static void output_character_function ();
  1444. static int compare_strings ();
  1445.  
  1446. /* Basic redisplay algorithm. */
  1447. rl_redisplay ()
  1448. {
  1449.   register int in, out, c, linenum;
  1450.   register char *line = invisible_line;
  1451.   char *prompt_this_line;
  1452.   int c_pos = 0;
  1453.   int inv_botlin = 0;        /* Number of lines in newly drawn buffer. */
  1454.  
  1455.   extern int readline_echoing_p;
  1456.  
  1457.   if (!readline_echoing_p)
  1458.     return;
  1459.  
  1460.   if (!rl_display_prompt)
  1461.     rl_display_prompt = "";
  1462.  
  1463.   if (!invisible_line)
  1464.     {
  1465.       visible_line = (char *)xmalloc (line_size);
  1466.       invisible_line = (char *)xmalloc (line_size);
  1467.       line = invisible_line;
  1468.       for (in = 0; in < line_size; in++)
  1469.     {
  1470.       visible_line[in] = 0;
  1471.       invisible_line[in] = 1;
  1472.     }
  1473.       rl_on_new_line ();
  1474.     }
  1475.  
  1476.   /* Draw the line into the buffer. */
  1477.   c_pos = -1;
  1478.  
  1479.   /* Mark the line as modified or not.  We only do this for history
  1480.      lines. */
  1481.   out = 0;
  1482.   if (mark_modified_lines && current_history () && rl_undo_list)
  1483.     {
  1484.       line[out++] = '*';
  1485.       line[out] = '\0';
  1486.     }
  1487.  
  1488.   /* If someone thought that the redisplay was handled, but the currently
  1489.      visible line has a different modification state than the one about
  1490.      to become visible, then correct the callers misconception. */
  1491.   if (visible_line[0] != invisible_line[0])
  1492.     rl_display_fixed = 0;
  1493.  
  1494.   prompt_this_line = rindex (rl_display_prompt, '\n');
  1495.   if (!prompt_this_line)
  1496.     prompt_this_line = rl_display_prompt;
  1497.   else
  1498.     {
  1499.       prompt_this_line++;
  1500.       if (forced_display)
  1501.     output_some_chars (rl_display_prompt,
  1502.                prompt_this_line - rl_display_prompt);
  1503.     }
  1504.  
  1505.   strncpy (line + out,  prompt_this_line, strlen (prompt_this_line));
  1506.   out += strlen (prompt_this_line);
  1507.   line[out] = '\0';
  1508.  
  1509.   for (in = 0; in < rl_end; in++)
  1510.     {
  1511.       c = (unsigned char)the_line[in];
  1512.  
  1513.       if (out + 1 >= line_size)
  1514.     {
  1515.       line_size *= 2;
  1516.       visible_line = (char *)xrealloc (visible_line, line_size);
  1517.       invisible_line = (char *)xrealloc (invisible_line, line_size);
  1518.       line = invisible_line;
  1519.     }
  1520.  
  1521.       if (in == rl_point)
  1522.     c_pos = out;
  1523.  
  1524.       if (c > 127)
  1525.     {
  1526.       line[out++] = 'M';
  1527.       line[out++] = '-';
  1528.       line[out++] = c - 128;
  1529.     }
  1530. #define DISPLAY_TABS
  1531. #if defined (DISPLAY_TABS)
  1532.       else if (c == '\t')
  1533.     {
  1534.       register int newout = (out | (int)7) + 1;
  1535.       while (out < newout)
  1536.         line[out++] = ' ';
  1537.     }
  1538. #endif
  1539.       else if (c < 32)
  1540.     {
  1541.       line[out++] = 'C';
  1542.       line[out++] = '-';
  1543.       line[out++] = c + 64;
  1544.     }
  1545.       else if (c == 127)
  1546.     {
  1547.       line[out++] = 'C';
  1548.       line[out++] = '-';
  1549.       line[out++] = '?';
  1550.     }
  1551.       else
  1552.     line[out++] = c;
  1553.     }
  1554.   line[out] = '\0';
  1555.   if (c_pos < 0)
  1556.     c_pos = out;
  1557.  
  1558.   /* PWP: now is when things get a bit hairy.  The visible and invisible
  1559.      line buffers are really multiple lines, which would wrap every
  1560.      (screenwidth - 1) characters.  Go through each in turn, finding
  1561.      the changed region and updating it.  The line order is top to bottom. */
  1562.  
  1563.   /* If we can move the cursor up and down, then use multiple lines,
  1564.      otherwise, let long lines display in a single terminal line, and
  1565.      horizontally scroll it. */
  1566.  
  1567.   if (!horizontal_scroll_mode && term_up && *term_up)
  1568.     {
  1569.       int total_screen_chars = (screenwidth * screenheight);
  1570.  
  1571.       if (!rl_display_fixed || forced_display)
  1572.     {
  1573.       forced_display = 0;
  1574.  
  1575.       /* If we have more than a screenful of material to display, then
  1576.          only display a screenful.  We should display the last screen,
  1577.          not the first.  I'll fix this in a minute. */
  1578.       if (out >= total_screen_chars)
  1579.         out = total_screen_chars - 1;
  1580.  
  1581.       /* Number of screen lines to display. */
  1582.       inv_botlin = out / screenwidth;
  1583.  
  1584.       /* For each line in the buffer, do the updating display. */
  1585.       for (linenum = 0; linenum <= inv_botlin; linenum++)
  1586.         update_line (linenum > vis_botlin ? ""
  1587.              : &visible_line[linenum * screenwidth],
  1588.              &invisible_line[linenum * screenwidth],
  1589.              linenum);
  1590.  
  1591.       /* We may have deleted some lines.  If so, clear the left over
  1592.          blank ones at the bottom out. */
  1593.       if (vis_botlin > inv_botlin)
  1594.         {
  1595.           char *tt;
  1596.           for (; linenum <= vis_botlin; linenum++)
  1597.         {
  1598.           tt = &visible_line[linenum * screenwidth];
  1599.           move_vert (linenum);
  1600.           move_cursor_relative (0, tt);
  1601.           clear_to_eol ((linenum == vis_botlin)?
  1602.                 strlen (tt) : screenwidth);
  1603.         }
  1604.         }
  1605.       vis_botlin = inv_botlin;
  1606.  
  1607.       /* Move the cursor where it should be. */
  1608.       move_vert (c_pos / screenwidth);
  1609.       move_cursor_relative (c_pos % screenwidth,
  1610.                 &invisible_line[(c_pos / screenwidth) * screenwidth]);
  1611.     }
  1612.     }
  1613.   else                /* Do horizontal scrolling. */
  1614.     {
  1615.       int lmargin;
  1616.  
  1617.       /* Always at top line. */
  1618.       last_v_pos = 0;
  1619.  
  1620.       /* If the display position of the cursor would be off the edge
  1621.      of the screen, start the display of this line at an offset that
  1622.      leaves the cursor on the screen. */
  1623.       if (c_pos - last_lmargin > screenwidth - 2)
  1624.     lmargin = (c_pos / (screenwidth / 3) - 2) * (screenwidth / 3);
  1625.       else if (c_pos - last_lmargin < 1)
  1626.     lmargin = ((c_pos - 1) / (screenwidth / 3)) * (screenwidth / 3);
  1627.       else
  1628.     lmargin = last_lmargin;
  1629.  
  1630.       /* If the first character on the screen isn't the first character
  1631.      in the display line, indicate this with a special character. */
  1632.       if (lmargin > 0)
  1633.     line[lmargin] = '<';
  1634.  
  1635.       if (lmargin + screenwidth < out)
  1636.     line[lmargin + screenwidth - 1] = '>';
  1637.  
  1638.       if (!rl_display_fixed || forced_display || lmargin != last_lmargin)
  1639.     {
  1640.       forced_display = 0;
  1641.       update_line (&visible_line[last_lmargin],
  1642.                &invisible_line[lmargin], 0);
  1643.  
  1644.       move_cursor_relative (c_pos - lmargin, &invisible_line[lmargin]);
  1645.       last_lmargin = lmargin;
  1646.     }
  1647.     }
  1648.   fflush (out_stream);
  1649.  
  1650.   /* Swap visible and non-visible lines. */
  1651.   {
  1652.     char *temp = visible_line;
  1653.     visible_line = invisible_line;
  1654.     invisible_line = temp;
  1655.     rl_display_fixed = 0;
  1656.   }
  1657. }
  1658.  
  1659. /* PWP: update_line() is based on finding the middle difference of each
  1660.    line on the screen; vis:
  1661.  
  1662.                  /old first difference
  1663.     /beginning of line   |              /old last same       /old EOL
  1664.     v             v              v                    v
  1665. old:    eddie> Oh, my little gruntle-buggy is to me, as lurgid as
  1666. new:    eddie> Oh, my little buggy says to me, as lurgid as
  1667.     ^             ^        ^               ^
  1668.     \beginning of line   |        \new last same       \new end of line
  1669.                  \new first difference
  1670.  
  1671.    All are character pointers for the sake of speed.  Special cases for
  1672.    no differences, as well as for end of line additions must be handeled.
  1673.  
  1674.    Could be made even smarter, but this works well enough */
  1675. static
  1676. update_line (old, new, current_line)
  1677.      register char *old, *new;
  1678.      int current_line;
  1679. {
  1680.   register char *ofd, *ols, *oe, *nfd, *nls, *ne;
  1681.   int lendiff, wsatend;
  1682.  
  1683.   /* Find first difference. */
  1684.   for (ofd = old, nfd = new;
  1685.        (ofd - old < screenwidth) && *ofd && (*ofd == *nfd);
  1686.        ofd++, nfd++)
  1687.     ;
  1688.  
  1689.   /* Move to the end of the screen line. */
  1690.   for (oe = ofd; ((oe - old) < screenwidth) && *oe; oe++);
  1691.   for (ne = nfd; ((ne - new) < screenwidth) && *ne; ne++);
  1692.  
  1693.   /* If no difference, continue to next line. */
  1694.   if (ofd == oe && nfd == ne)
  1695.     return;
  1696.  
  1697.   wsatend = 1;            /* flag for trailing whitespace */
  1698.   ols = oe - 1;            /* find last same */
  1699.   nls = ne - 1;
  1700.   while ((*ols == *nls) && (ols > ofd) && (nls > nfd))
  1701.     {
  1702.       if (*ols != ' ')
  1703.     wsatend = 0;
  1704.       ols--;
  1705.       nls--;
  1706.     }
  1707.  
  1708.   if (wsatend)
  1709.     {
  1710.       ols = oe;
  1711.       nls = ne;
  1712.     }
  1713.   else if (*ols != *nls)
  1714.     {
  1715.       if (*ols)            /* don't step past the NUL */
  1716.     ols++;
  1717.       if (*nls)
  1718.     nls++;
  1719.     }
  1720.  
  1721.   move_vert (current_line);
  1722.   move_cursor_relative (ofd - old, old);
  1723.  
  1724.   /* if (len (new) > len (old)) */
  1725.   lendiff = (nls - nfd) - (ols - ofd);
  1726.  
  1727.   /* Insert (diff(len(old),len(new)) ch */
  1728.   if (lendiff > 0)
  1729.     {
  1730.       if (terminal_can_insert)
  1731.     {
  1732.       extern char *term_IC;
  1733.  
  1734.       /* Sometimes it is cheaper to print the characters rather than
  1735.          use the terminal's capabilities. */
  1736.       if ((2 * (ne - nfd)) < lendiff && !term_IC)
  1737.         {
  1738.           output_some_chars (nfd, (ne - nfd));
  1739.           last_c_pos += (ne - nfd);
  1740.         }
  1741.       else
  1742.         {
  1743.           if (*ols)
  1744.         {
  1745.           insert_some_chars (nfd, lendiff);
  1746.           last_c_pos += lendiff;
  1747.         }
  1748.           else
  1749.         {
  1750.           /* At the end of a line the characters do not have to
  1751.              be "inserted".  They can just be placed on the screen. */
  1752.           output_some_chars (nfd, lendiff);
  1753.           last_c_pos += lendiff;
  1754.         }
  1755.           /* Copy (new) chars to screen from first diff to last match. */
  1756.           if (((nls - nfd) - lendiff) > 0)
  1757.         {
  1758.           output_some_chars (&nfd[lendiff], ((nls - nfd) - lendiff));
  1759.           last_c_pos += ((nls - nfd) - lendiff);
  1760.         }
  1761.         }
  1762.     }
  1763.       else
  1764.     {        /* cannot insert chars, write to EOL */
  1765.       output_some_chars (nfd, (ne - nfd));
  1766.       last_c_pos += (ne - nfd);
  1767.     }
  1768.     }
  1769.   else                /* Delete characters from line. */
  1770.     {
  1771.       /* If possible and inexpensive to use terminal deletion, then do so. */
  1772.       if (term_dc && (2 * (ne - nfd)) >= (-lendiff))
  1773.     {
  1774.       if (lendiff)
  1775.         delete_chars (-lendiff); /* delete (diff) characters */
  1776.  
  1777.       /* Copy (new) chars to screen from first diff to last match */
  1778.       if ((nls - nfd) > 0)
  1779.         {
  1780.           output_some_chars (nfd, (nls - nfd));
  1781.           last_c_pos += (nls - nfd);
  1782.         }
  1783.     }
  1784.       /* Otherwise, print over the existing material. */
  1785.       else
  1786.     {
  1787.       output_some_chars (nfd, (ne - nfd));
  1788.       last_c_pos += (ne - nfd);
  1789.       clear_to_eol ((oe - old) - (ne - new));
  1790.     }
  1791.     }
  1792. }
  1793.  
  1794. /* (PWP) tell the update routines that we have moved onto a
  1795.    new (empty) line. */
  1796. rl_on_new_line ()
  1797. {
  1798.   if (visible_line)
  1799.     visible_line[0] = '\0';
  1800.  
  1801.   last_c_pos = last_v_pos = 0;
  1802.   vis_botlin = last_lmargin = 0;
  1803. }
  1804.  
  1805. /* Actually update the display, period. */
  1806. rl_forced_update_display ()
  1807. {
  1808.   if (visible_line)
  1809.     {
  1810.       register char *temp = visible_line;
  1811.  
  1812.       while (*temp) *temp++ = '\0';
  1813.     }
  1814.   rl_on_new_line ();
  1815.   forced_display++;
  1816.   rl_redisplay ();
  1817. }
  1818.  
  1819. /* Move the cursor from last_c_pos to NEW, which are buffer indices.
  1820.    DATA is the contents of the screen line of interest; i.e., where
  1821.    the movement is being done. */
  1822. static void
  1823. move_cursor_relative (new, data)
  1824.      int new;
  1825.      char *data;
  1826. {
  1827.   register int i;
  1828.  
  1829.   /* It may be faster to output a CR, and then move forwards instead
  1830.      of moving backwards. */
  1831.   if (new + 1 < last_c_pos - new)
  1832.     {
  1833. #ifdef __MSDOS__
  1834.       putc('\r', out_stream);
  1835. #else
  1836.       tputs (term_cr, 1, output_character_function);
  1837. #endif
  1838.       last_c_pos = 0;
  1839.     }
  1840.  
  1841.   if (last_c_pos == new) return;
  1842.  
  1843.   if (last_c_pos < new)
  1844.     {
  1845.       /* Move the cursor forward.  We do it by printing the command
  1846.      to move the cursor forward if there is one, else print that
  1847.      portion of the output buffer again.  Which is cheaper? */
  1848.  
  1849.       /* The above comment is left here for posterity.  It is faster
  1850.      to print one character (non-control) than to print a control
  1851.      sequence telling the terminal to move forward one character.
  1852.      That kind of control is for people who don't know what the
  1853.      data is underneath the cursor. */
  1854. #if defined (HACK_TERMCAP_MOTION)
  1855.       extern char *term_forward_char;
  1856.  
  1857.       if (term_forward_char)
  1858.     for (i = last_c_pos; i < new; i++)
  1859.       tputs (term_forward_char, 1, output_character_function);
  1860.       else
  1861.     for (i = last_c_pos; i < new; i++)
  1862.       putc (data[i], out_stream);
  1863. #else
  1864.       for (i = last_c_pos; i < new; i++)
  1865.     putc (data[i], out_stream);
  1866. #endif                /* HACK_TERMCAP_MOTION */
  1867.     }
  1868.   else
  1869.     backspace (last_c_pos - new);
  1870.   last_c_pos = new;
  1871. }
  1872.  
  1873. /* PWP: move the cursor up or down. */
  1874. move_vert (to)
  1875.      int to;
  1876. {
  1877.   void output_character_function ();
  1878.   register int delta, i;
  1879.  
  1880.   if (last_v_pos == to) return;
  1881.  
  1882.   if (to > screenheight)
  1883.     return;
  1884.  
  1885. #ifdef __GO32__
  1886.   {
  1887.     int cur_r, cur_c;
  1888.     ScreenGetCursor(&cur_r, &cur_c);
  1889.     ScreenSetCursor(cur_r+to-last_v_pos, cur_c);
  1890.   }
  1891. #else /* __GO32__ */
  1892.   if ((delta = to - last_v_pos) > 0)
  1893.     {
  1894.       for (i = 0; i < delta; i++)
  1895.     putc ('\n', out_stream);
  1896.       tputs (term_cr, 1, output_character_function);
  1897.       last_c_pos = 0;
  1898.     }
  1899.   else
  1900.     {            /* delta < 0 */
  1901.       if (term_up && *term_up)
  1902.     for (i = 0; i < -delta; i++)
  1903.       tputs (term_up, 1, output_character_function);
  1904.     }
  1905. #endif /* __GO32__ */
  1906.   last_v_pos = to;        /* now to is here */
  1907. }
  1908.  
  1909. /* Physically print C on out_stream.  This is for functions which know
  1910.    how to optimize the display. */
  1911. rl_show_char (c)
  1912.      int c;
  1913. {
  1914.   if (c > 127)
  1915.     {
  1916.       fprintf (out_stream, "M-");
  1917.       c -= 128;
  1918.     }
  1919.  
  1920. #if defined (DISPLAY_TABS)
  1921.   if (c < 32 && c != '\t')
  1922. #else
  1923.   if (c < 32)
  1924. #endif
  1925.     {
  1926.  
  1927.       c += 64;
  1928.     }
  1929.  
  1930.   putc (c, out_stream);
  1931.   fflush (out_stream);
  1932. }
  1933.  
  1934. #if defined (DISPLAY_TABS)
  1935. int
  1936. rl_character_len (c, pos)
  1937.      register int c, pos;
  1938. {
  1939.   if (c < ' ' || c > 126)
  1940.     {
  1941.       if (c == '\t')
  1942.     return (((pos | (int)7) + 1) - pos);
  1943.       else
  1944.     return (3);
  1945.     }
  1946.   else
  1947.     return (1);
  1948. }
  1949. #else
  1950. int
  1951. rl_character_len (c)
  1952.      int c;
  1953. {
  1954.   if (c < ' ' || c > 126)
  1955.     return (3);
  1956.   else
  1957.     return (1);
  1958. }
  1959. #endif  /* DISPLAY_TAB */
  1960.  
  1961. /* How to print things in the "echo-area".  The prompt is treated as a
  1962.    mini-modeline. */
  1963. rl_message (string, arg1, arg2)
  1964.      char *string;
  1965. {
  1966.   sprintf (msg_buf, string, arg1, arg2);
  1967.   rl_display_prompt = msg_buf;
  1968.   rl_redisplay ();
  1969. }
  1970.  
  1971. /* How to clear things from the "echo-area". */
  1972. rl_clear_message ()
  1973. {
  1974.   rl_display_prompt = rl_prompt;
  1975.   rl_redisplay ();
  1976. }
  1977.  
  1978. /* **************************************************************** */
  1979. /*                                    */
  1980. /*            Terminal and Termcap                */
  1981. /*                                    */
  1982. /* **************************************************************** */
  1983.  
  1984. static char *term_buffer = (char *)NULL;
  1985. static char *term_string_buffer = (char *)NULL;
  1986.  
  1987. /* Non-zero means this terminal can't really do anything. */
  1988. int dumb_term = 0;
  1989.  
  1990. char PC;
  1991. char *BC, *UP;
  1992.  
  1993. /* Some strings to control terminal actions.  These are output by tputs (). */
  1994. char *term_goto, *term_clreol, *term_cr, *term_clrpag, *term_backspace;
  1995.  
  1996. int screenwidth, screenheight;
  1997.  
  1998. /* Non-zero if we determine that the terminal can do character insertion. */
  1999. int terminal_can_insert = 0;
  2000.  
  2001. /* How to insert characters. */
  2002. char *term_im, *term_ei, *term_ic, *term_ip, *term_IC;
  2003.  
  2004. /* How to delete characters. */
  2005. char *term_dc, *term_DC;
  2006.  
  2007. #if defined (HACK_TERMCAP_MOTION)
  2008. char *term_forward_char;
  2009. #endif  /* HACK_TERMCAP_MOTION */
  2010.  
  2011. /* How to go up a line. */
  2012. char *term_up;
  2013.  
  2014. /* A visible bell, if the terminal can be made to flash the screen. */
  2015. char *visible_bell;
  2016.  
  2017. /* Re-initialize the terminal considering that the TERM/TERMCAP variable
  2018.    has changed. */
  2019. rl_reset_terminal (terminal_name)
  2020.      char *terminal_name;
  2021. {
  2022.   init_terminal_io (terminal_name);
  2023. }
  2024.  
  2025. init_terminal_io (terminal_name)
  2026.      char *terminal_name;
  2027. {
  2028. #ifdef __GO32__
  2029.   screenwidth = ScreenCols();
  2030.   screenheight = ScreenRows();
  2031.   term_cr = "\r";
  2032.   term_im = term_ei = term_ic = term_IC = (char *)NULL;
  2033.   term_up = term_dc = term_DC = visible_bell = (char *)NULL;
  2034. #if defined (HACK_TERMCAP_MOTION)
  2035.       term_forward_char = (char *)NULL;
  2036. #endif
  2037.   terminal_can_insert = 0;
  2038.   return;
  2039. #else
  2040.   extern char *tgetstr ();
  2041.   char *term, *buffer;
  2042. #if defined (TIOCGWINSZ)
  2043.   struct winsize window_size;
  2044. #endif
  2045.   int tty;
  2046.  
  2047.   term = terminal_name ? terminal_name : getenv ("TERM");
  2048.  
  2049.   if (!term_string_buffer)
  2050.     term_string_buffer = (char *)xmalloc (2048);
  2051.  
  2052.   if (!term_buffer)
  2053.     term_buffer = (char *)xmalloc (2048);
  2054.  
  2055.   buffer = term_string_buffer;
  2056.  
  2057.   term_clrpag = term_cr = term_clreol = (char *)NULL;
  2058.  
  2059.   if (!term)
  2060.     term = "dumb";
  2061.  
  2062.   if (tgetent (term_buffer, term) < 0)
  2063.     {
  2064.       dumb_term = 1;
  2065.       screenwidth = 79;
  2066.       screenheight = 24;
  2067.       term_cr = "\r";
  2068.       term_im = term_ei = term_ic = term_IC = (char *)NULL;
  2069.       term_up = term_dc = term_DC = visible_bell = (char *)NULL;
  2070. #if defined (HACK_TERMCAP_MOTION)
  2071.       term_forward_char = (char *)NULL;
  2072. #endif
  2073.       terminal_can_insert = 0;
  2074.       return;
  2075.     }
  2076.  
  2077.   BC = tgetstr ("pc", &buffer);
  2078.   PC = buffer ? *buffer : 0;
  2079.  
  2080.   term_backspace = tgetstr ("le", &buffer);
  2081.  
  2082.   term_cr = tgetstr ("cr", &buffer);
  2083.   term_clreol = tgetstr ("ce", &buffer);
  2084.   term_clrpag = tgetstr ("cl", &buffer);
  2085.  
  2086.   if (!term_cr)
  2087.     term_cr =  "\r";
  2088.  
  2089. #if defined (HACK_TERMCAP_MOTION)
  2090.   term_forward_char = tgetstr ("nd", &buffer);
  2091. #endif  /* HACK_TERMCAP_MOTION */
  2092.  
  2093.   if (rl_instream)
  2094.     tty = fileno (rl_instream);
  2095.   else
  2096.     tty = 0;
  2097.  
  2098.   screenwidth = screenheight = 0;
  2099. #if defined (TIOCGWINSZ)
  2100.   if (ioctl (tty, TIOCGWINSZ, &window_size) == 0)
  2101.     {
  2102.       screenwidth = (int) window_size.ws_col;
  2103.       screenheight = (int) window_size.ws_row;
  2104.     }
  2105. #endif
  2106.  
  2107.   if (screenwidth <= 0 || screenheight <= 0)
  2108.     {
  2109.       screenwidth = tgetnum ("co");
  2110.       screenheight = tgetnum ("li");
  2111.     }
  2112.  
  2113.   screenwidth--;
  2114.  
  2115.   if (screenwidth <= 0)
  2116.     screenwidth = 79;
  2117.  
  2118.   if (screenheight <= 0)
  2119.     screenheight = 24;
  2120.  
  2121.   term_im = tgetstr ("im", &buffer);
  2122.   term_ei = tgetstr ("ei", &buffer);
  2123.   term_IC = tgetstr ("IC", &buffer);
  2124.   term_ic = tgetstr ("ic", &buffer);
  2125.  
  2126.   /* "An application program can assume that the terminal can do
  2127.       character insertion if *any one of* the capabilities `IC',
  2128.       `im', `ic' or `ip' is provided."  But we can't do anything if
  2129.       only `ip' is provided, so... */
  2130.   terminal_can_insert = (term_IC || term_im || term_ic);
  2131.  
  2132.   term_up = tgetstr ("up", &buffer);
  2133.   term_dc = tgetstr ("dc", &buffer);
  2134.   term_DC = tgetstr ("DC", &buffer);
  2135.  
  2136.   visible_bell = tgetstr ("vb", &buffer);
  2137. #endif /* !__GO32__ */
  2138. }
  2139.  
  2140. /* A function for the use of tputs () */
  2141. static void
  2142. output_character_function (c)
  2143.      int c;
  2144. {
  2145.   putc (c, out_stream);
  2146. }
  2147.  
  2148. /* Write COUNT characters from STRING to the output stream. */
  2149. static void
  2150. output_some_chars (string, count)
  2151.      char *string;
  2152.      int count;
  2153. {
  2154.   fwrite (string, 1, count, out_stream);
  2155. }
  2156.  
  2157. /* Delete COUNT characters from the display line. */
  2158. static
  2159. delete_chars (count)
  2160.      int count;
  2161. {
  2162. #ifdef __GO32__
  2163.   int r, c, w;
  2164.   ScreenGetCursor(&r, &c);
  2165.   w = ScreenCols();
  2166.   memcpy(ScreenPrimary+r*w+c, ScreenPrimary+r*w+c+count, w-c-count);
  2167.   memset(ScreenPrimary+r*w+w-count, 0, count*2);
  2168. #else /* __GO32__ */
  2169.   if (count > screenwidth)
  2170.     return;
  2171.  
  2172.   if (term_DC && *term_DC)
  2173.     {
  2174.       char *tgoto (), *buffer;
  2175.       buffer = tgoto (term_DC, 0, count);
  2176.       tputs (buffer, 1, output_character_function);
  2177.     }
  2178.   else
  2179.     {
  2180.       if (term_dc && *term_dc)
  2181.     while (count--)
  2182.       tputs (term_dc, 1, output_character_function);
  2183.     }
  2184. #endif /* __GO32__ */
  2185. }
  2186.  
  2187. /* Insert COUNT characters from STRING to the output stream. */
  2188. static
  2189. insert_some_chars (string, count)
  2190.      char *string;
  2191.      int count;
  2192. {
  2193. #ifdef __GO32__
  2194.   int r, c, w;
  2195.   ScreenGetCursor(&r, &c);
  2196.   w = ScreenCols();
  2197.   memcpy(ScreenPrimary+r*w+c+count, ScreenPrimary+r*w+c, w-c-count);
  2198.   /* Print the text. */
  2199.   output_some_chars (string, count);
  2200. #else /* __GO32__ */
  2201.   /* If IC is defined, then we do not have to "enter" insert mode. */
  2202.   if (term_IC)
  2203.     {
  2204.       char *tgoto (), *buffer;
  2205.       buffer = tgoto (term_IC, 0, count);
  2206.       tputs (buffer, 1, output_character_function);
  2207.       output_some_chars (string, count);
  2208.     }
  2209.   else
  2210.     {
  2211.       register int i;
  2212.  
  2213.       /* If we have to turn on insert-mode, then do so. */
  2214.       if (term_im && *term_im)
  2215.     tputs (term_im, 1, output_character_function);
  2216.  
  2217.       /* If there is a special command for inserting characters, then
  2218.      use that first to open up the space. */
  2219.       if (term_ic && *term_ic)
  2220.     {
  2221.       for (i = count; i--; )
  2222.         tputs (term_ic, 1, output_character_function);
  2223.     }
  2224.  
  2225.       /* Print the text. */
  2226.       output_some_chars (string, count);
  2227.  
  2228.       /* If there is a string to turn off insert mode, we had best use
  2229.      it now. */
  2230.       if (term_ei && *term_ei)
  2231.     tputs (term_ei, 1, output_character_function);
  2232.     }
  2233. #endif /* __GO32__ */
  2234. }
  2235.  
  2236. /* Move the cursor back. */
  2237. backspace (count)
  2238.      int count;
  2239. {
  2240.   register int i;
  2241.  
  2242. #ifndef __GO32__
  2243.   if (term_backspace)
  2244.     for (i = 0; i < count; i++)
  2245.       tputs (term_backspace, 1, output_character_function);
  2246.   else
  2247. #endif /* !__GO32__ */
  2248.     for (i = 0; i < count; i++)
  2249.       putc ('\b', out_stream);
  2250. }
  2251.  
  2252. /* Move to the start of the next line. */
  2253. crlf ()
  2254. {
  2255. #if defined (NEW_TTY_DRIVER)
  2256.   tputs (term_cr, 1, output_character_function);
  2257. #endif /* NEW_TTY_DRIVER */
  2258.   putc ('\n', out_stream);
  2259. }
  2260.  
  2261. /* Clear to the end of the line.  COUNT is the minimum
  2262.    number of character spaces to clear, */
  2263. clear_to_eol (count)
  2264.      int count;
  2265. {
  2266. #ifndef __GO32__
  2267.   if (term_clreol)
  2268.     {
  2269.       tputs (term_clreol, 1, output_character_function);
  2270.     }
  2271.   else
  2272. #endif /* !__GO32__ */
  2273.     {
  2274.       register int i;
  2275.  
  2276.       /* Do one more character space. */
  2277.       count++;
  2278.  
  2279.       for (i = 0; i < count; i++)
  2280.     putc (' ', out_stream);
  2281.  
  2282.       backspace (count);
  2283.     }
  2284. }
  2285.  
  2286.  
  2287. /* **************************************************************** */
  2288. /*                                    */
  2289. /*              Saving and Restoring the TTY                */
  2290. /*                                    */
  2291. /* **************************************************************** */
  2292.  
  2293. /* Non-zero means that the terminal is in a prepped state. */
  2294. static int terminal_prepped = 0;
  2295.  
  2296. #if defined (NEW_TTY_DRIVER)
  2297.  
  2298. /* Standard flags, including ECHO. */
  2299. static int original_tty_flags = 0;
  2300.  
  2301. /* Local mode flags, like LPASS8. */
  2302. static int local_mode_flags = 0;
  2303.  
  2304. /* Terminal characters.  This has C-s and C-q in it. */
  2305. static struct tchars original_tchars;
  2306.  
  2307. /* Local special characters.  This has the interrupt characters in it. */
  2308. #if defined (TIOCGLTC)
  2309. static struct ltchars original_ltchars;
  2310. #endif
  2311.  
  2312. /* We use this to get and set the tty_flags. */
  2313. static struct sgttyb the_ttybuff;
  2314.  
  2315. /* Put the terminal in CBREAK mode so that we can detect key presses. */
  2316. static void
  2317. rl_prep_terminal ()
  2318. {
  2319. #ifndef __GO32__
  2320.   int tty = fileno (rl_instream);
  2321. #if defined (HAVE_BSD_SIGNALS)
  2322.   int oldmask;
  2323. #endif /* HAVE_BSD_SIGNALS */
  2324.  
  2325.   if (terminal_prepped)
  2326.     return;
  2327.  
  2328.   oldmask = sigblock (sigmask (SIGINT));
  2329.  
  2330.   /* We always get the latest tty values.  Maybe stty changed them. */
  2331.   ioctl (tty, TIOCGETP, &the_ttybuff);
  2332.   original_tty_flags = the_ttybuff.sg_flags;
  2333.  
  2334.   readline_echoing_p = (original_tty_flags & ECHO);
  2335.  
  2336. #if defined (TIOCLGET)
  2337.   ioctl (tty, TIOCLGET, &local_mode_flags);
  2338. #endif
  2339.  
  2340. #if !defined (ANYP)
  2341. #  define ANYP (EVENP | ODDP)
  2342. #endif
  2343.  
  2344.   /* If this terminal doesn't care how the 8th bit is used,
  2345.      then we can use it for the meta-key.  We check by seeing
  2346.      if BOTH odd and even parity are allowed. */
  2347.   if (the_ttybuff.sg_flags & ANYP)
  2348.     {
  2349. #if defined (PASS8)
  2350.       the_ttybuff.sg_flags |= PASS8;
  2351. #endif
  2352.  
  2353.       /* Hack on local mode flags if we can. */
  2354. #if defined (TIOCLGET) && defined (LPASS8)
  2355.       {
  2356.     int flags;
  2357.     flags = local_mode_flags | LPASS8;
  2358.     ioctl (tty, TIOCLSET, &flags);
  2359.       }
  2360. #endif /* TIOCLGET && LPASS8 */
  2361.     }
  2362.  
  2363. #if defined (TIOCGETC)
  2364.   {
  2365.     struct tchars temp;
  2366.  
  2367.     ioctl (tty, TIOCGETC, &original_tchars);
  2368.     temp = original_tchars;
  2369.  
  2370. #if defined (USE_XON_XOFF)
  2371.     /* Get rid of C-s and C-q.
  2372.        We remember the value of startc (C-q) so that if the terminal is in
  2373.        xoff state, the user can xon it by pressing that character. */
  2374.     xon_char = temp.t_startc;
  2375.     temp.t_stopc = -1;
  2376.     temp.t_startc = -1;
  2377.  
  2378.     /* If there is an XON character, bind it to restart the output. */
  2379.     if (xon_char != -1)
  2380.       rl_bind_key (xon_char, rl_restart_output);
  2381. #endif /* USE_XON_XOFF */
  2382.  
  2383.     /* If there is an EOF char, bind eof_char to it. */
  2384.     if (temp.t_eofc != -1)
  2385.       eof_char = temp.t_eofc;
  2386.  
  2387. #if defined (NO_KILL_INTR)
  2388.     /* Get rid of C-\ and C-c. */
  2389.     temp.t_intrc = temp.t_quitc = -1;
  2390. #endif /* NO_KILL_INTR */
  2391.  
  2392.     ioctl (tty, TIOCSETC, &temp);
  2393.   }
  2394. #endif /* TIOCGETC */
  2395.  
  2396. #if defined (TIOCGLTC)
  2397.   {
  2398.     struct ltchars temp;
  2399.  
  2400.     ioctl (tty, TIOCGLTC, &original_ltchars);
  2401.     temp = original_ltchars;
  2402.  
  2403.     /* Make the interrupt keys go away.  Just enough to make people
  2404.        happy. */
  2405.     temp.t_dsuspc = -1;    /* C-y */
  2406.     temp.t_lnextc = -1;    /* C-v */
  2407.  
  2408.     ioctl (tty, TIOCSLTC, &temp);
  2409.   }
  2410. #endif /* TIOCGLTC */
  2411.  
  2412.   the_ttybuff.sg_flags &= ~(ECHO | CRMOD);
  2413.   the_ttybuff.sg_flags |= CBREAK;
  2414.   ioctl (tty, TIOCSETN, &the_ttybuff);
  2415.  
  2416.   terminal_prepped = 1;
  2417.  
  2418. #if defined (HAVE_BSD_SIGNALS)
  2419.   sigsetmask (oldmask);
  2420. #endif
  2421. #endif /* !__GO32__ */
  2422. }
  2423.  
  2424. /* Restore the terminal to its original state. */
  2425. static void
  2426. rl_deprep_terminal ()
  2427. {
  2428. #ifndef __GO32__
  2429.   int tty = fileno (rl_instream);
  2430. #if defined (HAVE_BSD_SIGNALS)
  2431.   int oldmask;
  2432. #endif
  2433.  
  2434.   if (!terminal_prepped)
  2435.     return;
  2436.  
  2437.   oldmask = sigblock (sigmask (SIGINT));
  2438.  
  2439.   the_ttybuff.sg_flags = original_tty_flags;
  2440.   ioctl (tty, TIOCSETN, &the_ttybuff);
  2441.   readline_echoing_p = 1;
  2442.  
  2443. #if defined (TIOCLGET)
  2444.   ioctl (tty, TIOCLSET, &local_mode_flags);
  2445. #endif
  2446.  
  2447. #if defined (TIOCSLTC)
  2448.   ioctl (tty, TIOCSLTC, &original_ltchars);
  2449. #endif
  2450.  
  2451. #if defined (TIOCSETC)
  2452.   ioctl (tty, TIOCSETC, &original_tchars);
  2453. #endif
  2454.   terminal_prepped = 0;
  2455.  
  2456. #if defined (HAVE_BSD_SIGNALS)
  2457.   sigsetmask (oldmask);
  2458. #endif
  2459. #endif /* !__GO32 */
  2460. }
  2461.  
  2462. #else  /* !defined (NEW_TTY_DRIVER) */
  2463.  
  2464. #if !defined (VMIN)
  2465. #define VMIN VEOF
  2466. #endif
  2467.  
  2468. #if !defined (VTIME)
  2469. #define VTIME VEOL
  2470. #endif
  2471.  
  2472. #ifndef __GO32__
  2473. #if defined (TERMIOS_TTY_DRIVER)
  2474. static struct termios otio;
  2475. #else
  2476. static struct termio otio;
  2477. #endif /* !TERMIOS_TTY_DRIVER */
  2478. #endif /* __GO32__ */
  2479.  
  2480. static void
  2481. rl_prep_terminal ()
  2482. {
  2483. #ifndef __GO32__
  2484.   int tty = fileno (rl_instream);
  2485. #if defined (TERMIOS_TTY_DRIVER)
  2486.   struct termios tio;
  2487. #else
  2488.   struct termio tio;
  2489. #endif /* !TERMIOS_TTY_DRIVER */
  2490.  
  2491. #if defined (HAVE_POSIX_SIGNALS)
  2492.   sigset_t set, oset;
  2493. #else
  2494. #  if defined (HAVE_BSD_SIGNALS)
  2495.   int oldmask;
  2496. #  endif /* HAVE_BSD_SIGNALS */
  2497. #endif /* !HAVE_POSIX_SIGNALS */
  2498.  
  2499.   if (terminal_prepped)
  2500.     return;
  2501.  
  2502.   /* Try to keep this function from being INTerrupted.  We can do it
  2503.      on POSIX and systems with BSD-like signal handling. */
  2504. #if defined (HAVE_POSIX_SIGNALS)
  2505.   sigemptyset (&set);
  2506.   sigaddset (&set, SIGINT);
  2507.   sigprocmask (SIG_BLOCK, &set, &oset);
  2508. #else /* !HAVE_POSIX_SIGNALS */
  2509. #  if defined (HAVE_BSD_SIGNALS)
  2510.   oldmask = sigblock (sigmask (SIGINT));
  2511. #  endif /* HAVE_BSD_SIGNALS */
  2512. #endif /* !HAVE_POSIX_SIGNALS */
  2513.  
  2514. #if defined (TERMIOS_TTY_DRIVER)
  2515.   tcgetattr (tty, &tio);
  2516. #else
  2517.   ioctl (tty, TCGETA, &tio);
  2518. #endif /* !TERMIOS_TTY_DRIVER */
  2519.  
  2520.   otio = tio;
  2521.  
  2522.   readline_echoing_p = (tio.c_lflag & ECHO);
  2523.  
  2524.   tio.c_lflag &= ~(ICANON|ECHO);
  2525.  
  2526.   if (otio.c_cc[VEOF] != _POSIX_VDISABLE)
  2527.     eof_char = otio.c_cc[VEOF];
  2528.  
  2529. #if defined (USE_XON_XOFF)
  2530. #if defined (IXANY)
  2531.   tio.c_iflag &= ~(IXON|IXOFF|IXANY);
  2532. #else
  2533.   /* `strict' Posix systems do not define IXANY. */
  2534.   tio.c_iflag &= ~(IXON|IXOFF);
  2535. #endif /* IXANY */
  2536. #endif /* USE_XON_XOFF */
  2537.  
  2538.   /* Only turn this off if we are using all 8 bits. */
  2539.   /* |ISTRIP|INPCK */
  2540.   tio.c_iflag &= ~(ISTRIP | INPCK);
  2541.  
  2542.   /* Make sure we differentiate between CR and NL on input. */
  2543.   tio.c_iflag &= ~(ICRNL | INLCR);
  2544.  
  2545. #if !defined (HANDLE_SIGNALS)
  2546.   tio.c_lflag &= ~ISIG;
  2547. #else
  2548.   tio.c_lflag |= ISIG;
  2549. #endif
  2550.  
  2551.   tio.c_cc[VMIN] = 1;
  2552.   tio.c_cc[VTIME] = 0;
  2553.  
  2554.   /* Turn off characters that we need on Posix systems with job control,
  2555.      just to be sure.  This includes ^Y and ^V.  This should not really
  2556.      be necessary.  */
  2557. #if defined (TERMIOS_TTY_DRIVER) && defined (_POSIX_JOB_CONTROL)
  2558.  
  2559. #if defined (VLNEXT)
  2560.   tio.c_cc[VLNEXT] = _POSIX_VDISABLE;
  2561. #endif
  2562.  
  2563. #if defined (VDSUSP)
  2564.   tio.c_cc[VDSUSP] = _POSIX_VDISABLE;
  2565. #endif
  2566.  
  2567. #endif /* POSIX && JOB_CONTROL */
  2568.  
  2569. #if defined (TERMIOS_TTY_DRIVER)
  2570.   tcsetattr (tty, TCSADRAIN, &tio);
  2571.   tcflow (tty, TCOON);        /* Simulate a ^Q. */
  2572. #else
  2573.   ioctl (tty, TCSETAW, &tio);
  2574.   ioctl (tty, TCXONC, 1);    /* Simulate a ^Q. */
  2575. #endif /* !TERMIOS_TTY_DRIVER */
  2576.  
  2577.   terminal_prepped = 1;
  2578.  
  2579. #if defined (HAVE_POSIX_SIGNALS)
  2580.   sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL);
  2581. #else
  2582. #  if defined (HAVE_BSD_SIGNALS)
  2583.   sigsetmask (oldmask);
  2584. #  endif /* HAVE_BSD_SIGNALS */
  2585. #endif /* !HAVE_POSIX_SIGNALS */
  2586. #endif /* !__GO32__ */
  2587. }
  2588.  
  2589. static void
  2590. rl_deprep_terminal ()
  2591. {
  2592. #ifndef __GO32__ 
  2593.   int tty = fileno (rl_instream);
  2594.  
  2595.   /* Try to keep this function from being INTerrupted.  We can do it
  2596.      on POSIX and systems with BSD-like signal handling. */
  2597. #if defined (HAVE_POSIX_SIGNALS)
  2598.   sigset_t set, oset;
  2599. #else /* !HAVE_POSIX_SIGNALS */
  2600. #  if defined (HAVE_BSD_SIGNALS)
  2601.   int oldmask;
  2602. #  endif /* HAVE_BSD_SIGNALS */
  2603. #endif /* !HAVE_POSIX_SIGNALS */
  2604.  
  2605.   if (!terminal_prepped)
  2606.     return;
  2607.  
  2608. #if defined (HAVE_POSIX_SIGNALS)
  2609.   sigemptyset (&set);
  2610.   sigaddset (&set, SIGINT);
  2611.   sigprocmask (SIG_BLOCK, &set, &oset);
  2612. #else /* !HAVE_POSIX_SIGNALS */
  2613. #  if defined (HAVE_BSD_SIGNALS)
  2614.   oldmask = sigblock (sigmask (SIGINT));
  2615. #  endif /* HAVE_BSD_SIGNALS */
  2616. #endif /* !HAVE_POSIX_SIGNALS */
  2617.  
  2618. #if defined (TERMIOS_TTY_DRIVER)
  2619.   tcsetattr (tty, TCSADRAIN, &otio);
  2620.   tcflow (tty, TCOON);        /* Simulate a ^Q. */
  2621. #else /* TERMIOS_TTY_DRIVER */
  2622.   ioctl (tty, TCSETAW, &otio);
  2623.   ioctl (tty, TCXONC, 1);    /* Simulate a ^Q. */
  2624. #endif /* !TERMIOS_TTY_DRIVER */
  2625.  
  2626.   terminal_prepped = 0;
  2627.  
  2628. #if defined (HAVE_POSIX_SIGNALS)
  2629.   sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL);
  2630. #else /* !HAVE_POSIX_SIGNALS */
  2631. #  if defined (HAVE_BSD_SIGNALS)
  2632.   sigsetmask (oldmask);
  2633. #  endif /* HAVE_BSD_SIGNALS */
  2634. #endif /* !HAVE_POSIX_SIGNALS */
  2635. #endif /* !__GO32__ */
  2636. }
  2637. #endif  /* NEW_TTY_DRIVER */
  2638.  
  2639.  
  2640. /* **************************************************************** */
  2641. /*                                    */
  2642. /*            Utility Functions                */
  2643. /*                                    */
  2644. /* **************************************************************** */
  2645.  
  2646. /* Return 0 if C is not a member of the class of characters that belong
  2647.    in words, or 1 if it is. */
  2648.  
  2649. int allow_pathname_alphabetic_chars = 0;
  2650. char *pathname_alphabetic_chars = "/-_=~.#$";
  2651.  
  2652. int
  2653. alphabetic (c)
  2654.      int c;
  2655. {
  2656.   if (pure_alphabetic (c) || (numeric (c)))
  2657.     return (1);
  2658.  
  2659.   if (allow_pathname_alphabetic_chars)
  2660.     return ((int)rindex (pathname_alphabetic_chars, c));
  2661.   else
  2662.     return (0);
  2663. }
  2664.  
  2665. /* Return non-zero if C is a numeric character. */
  2666. int
  2667. numeric (c)
  2668.      int c;
  2669. {
  2670.   return (c >= '0' && c <= '9');
  2671. }
  2672.  
  2673. /* Ring the terminal bell. */
  2674. int
  2675. ding ()
  2676. {
  2677.   if (readline_echoing_p)
  2678.     {
  2679. #ifndef __GO32__
  2680.       if (prefer_visible_bell && visible_bell)
  2681.     tputs (visible_bell, 1, output_character_function);
  2682.       else
  2683. #endif /* !__GO32__ */
  2684.     {
  2685.       fprintf (stderr, "\007");
  2686.       fflush (stderr);
  2687.     }
  2688.     }
  2689.   return (-1);
  2690. }
  2691.  
  2692. /* How to abort things. */
  2693. rl_abort ()
  2694. {
  2695.   ding ();
  2696.   rl_clear_message ();
  2697.   rl_init_argument ();
  2698.   rl_pending_input = 0;
  2699.  
  2700.   defining_kbd_macro = 0;
  2701.   while (executing_macro)
  2702.     pop_executing_macro ();
  2703.  
  2704.   rl_last_func = (Function *)NULL;
  2705.   longjmp (readline_top_level, 1);
  2706. }
  2707.  
  2708. /* Return a copy of the string between FROM and TO.
  2709.    FROM is inclusive, TO is not. */
  2710. #if defined (sun) /* Yes, that's right, some crufty function in sunview is
  2711.              called rl_copy (). */
  2712. static
  2713. #endif
  2714. char *
  2715. rl_copy (from, to)
  2716.      int from, to;
  2717. {
  2718.   register int length;
  2719.   char *copy;
  2720.  
  2721.   /* Fix it if the caller is confused. */
  2722.   if (from > to)
  2723.     {
  2724.       int t = from;
  2725.       from = to;
  2726.       to = t;
  2727.     }
  2728.  
  2729.   length = to - from;
  2730.   copy = (char *)xmalloc (1 + length);
  2731.   strncpy (copy, the_line + from, length);
  2732.   copy[length] = '\0';
  2733.   return (copy);
  2734. }
  2735.  
  2736. /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
  2737.    LEN characters. */
  2738. void
  2739. rl_extend_line_buffer (len)
  2740.      int len;
  2741. {
  2742.   while (len >= rl_line_buffer_len)
  2743.     rl_line_buffer =
  2744.       (char *)xrealloc
  2745.     (rl_line_buffer, rl_line_buffer_len += DEFAULT_BUFFER_SIZE);
  2746.  
  2747.   the_line = rl_line_buffer;
  2748. }
  2749.  
  2750.  
  2751. /* **************************************************************** */
  2752. /*                                    */
  2753. /*            Insert and Delete                */
  2754. /*                                    */
  2755. /* **************************************************************** */
  2756.  
  2757. /* Insert a string of text into the line at point.  This is the only
  2758.    way that you should do insertion.  rl_insert () calls this
  2759.    function. */
  2760. rl_insert_text (string)
  2761.      char *string;
  2762. {
  2763.   extern int doing_an_undo;
  2764.   register int i, l = strlen (string);
  2765.  
  2766.   if (rl_end + l >= rl_line_buffer_len)
  2767.     rl_extend_line_buffer (rl_end + l);
  2768.  
  2769.   for (i = rl_end; i >= rl_point; i--)
  2770.     the_line[i + l] = the_line[i];
  2771.   strncpy (the_line + rl_point, string, l);
  2772.  
  2773.   /* Remember how to undo this if we aren't undoing something. */
  2774.   if (!doing_an_undo)
  2775.     {
  2776.       /* If possible and desirable, concatenate the undos. */
  2777.       if ((strlen (string) == 1) &&
  2778.       rl_undo_list &&
  2779.       (rl_undo_list->what == UNDO_INSERT) &&
  2780.       (rl_undo_list->end == rl_point) &&
  2781.       (rl_undo_list->end - rl_undo_list->start < 20))
  2782.     rl_undo_list->end++;
  2783.       else
  2784.     rl_add_undo (UNDO_INSERT, rl_point, rl_point + l, (char *)NULL);
  2785.     }
  2786.   rl_point += l;
  2787.   rl_end += l;
  2788.   the_line[rl_end] = '\0';
  2789. }
  2790.  
  2791. /* Delete the string between FROM and TO.  FROM is
  2792.    inclusive, TO is not. */
  2793. rl_delete_text (from, to)
  2794.      int from, to;
  2795. {
  2796.   extern int doing_an_undo;
  2797.   register char *text;
  2798.  
  2799.   /* Fix it if the caller is confused. */
  2800.   if (from > to)
  2801.     {
  2802.       int t = from;
  2803.       from = to;
  2804.       to = t;
  2805.     }
  2806.   text = rl_copy (from, to);
  2807.   strncpy (the_line + from, the_line + to, rl_end - to);
  2808.  
  2809.   /* Remember how to undo this delete. */
  2810.   if (!doing_an_undo)
  2811.     rl_add_undo (UNDO_DELETE, from, to, text);
  2812.   else
  2813.     free (text);
  2814.  
  2815.   rl_end -= (to - from);
  2816.   the_line[rl_end] = '\0';
  2817. }
  2818.  
  2819.  
  2820. /* **************************************************************** */
  2821. /*                                    */
  2822. /*            Readline character functions            */
  2823. /*                                    */
  2824. /* **************************************************************** */
  2825.  
  2826. /* This is not a gap editor, just a stupid line input routine.  No hair
  2827.    is involved in writing any of the functions, and none should be. */
  2828.  
  2829. /* Note that:
  2830.  
  2831.    rl_end is the place in the string that we would place '\0';
  2832.    i.e., it is always safe to place '\0' there.
  2833.  
  2834.    rl_point is the place in the string where the cursor is.  Sometimes
  2835.    this is the same as rl_end.
  2836.  
  2837.    Any command that is called interactively receives two arguments.
  2838.    The first is a count: the numeric arg pased to this command.
  2839.    The second is the key which invoked this command.
  2840. */
  2841.  
  2842.  
  2843. /* **************************************************************** */
  2844. /*                                    */
  2845. /*            Movement Commands                */
  2846. /*                                    */
  2847. /* **************************************************************** */
  2848.  
  2849. /* Note that if you `optimize' the display for these functions, you cannot
  2850.    use said functions in other functions which do not do optimizing display.
  2851.    I.e., you will have to update the data base for rl_redisplay, and you
  2852.    might as well let rl_redisplay do that job. */
  2853.  
  2854. /* Move forward COUNT characters. */
  2855. rl_forward (count)
  2856.      int count;
  2857. {
  2858.   if (count < 0)
  2859.     rl_backward (-count);
  2860.   else
  2861.     while (count)
  2862.       {
  2863. #if defined (VI_MODE)
  2864.     if (rl_point == (rl_end - (rl_editing_mode == vi_mode)))
  2865. #else
  2866.     if (rl_point == rl_end)
  2867. #endif /* VI_MODE */
  2868.       {
  2869.         ding ();
  2870.         return;
  2871.       }
  2872.     else
  2873.       rl_point++;
  2874.     --count;
  2875.       }
  2876. }
  2877.  
  2878. /* Move backward COUNT characters. */
  2879. rl_backward (count)
  2880.      int count;
  2881. {
  2882.   if (count < 0)
  2883.     rl_forward (-count);
  2884.   else
  2885.     while (count)
  2886.       {
  2887.     if (!rl_point)
  2888.       {
  2889.         ding ();
  2890.         return;
  2891.       }
  2892.     else
  2893.       --rl_point;
  2894.     --count;
  2895.       }
  2896. }
  2897.  
  2898. /* Move to the beginning of the line. */
  2899. rl_beg_of_line ()
  2900. {
  2901.   rl_point = 0;
  2902. }
  2903.  
  2904. /* Move to the end of the line. */
  2905. rl_end_of_line ()
  2906. {
  2907.   rl_point = rl_end;
  2908. }
  2909.  
  2910. /* Move forward a word.  We do what Emacs does. */
  2911. rl_forward_word (count)
  2912.      int count;
  2913. {
  2914.   int c;
  2915.  
  2916.   if (count < 0)
  2917.     {
  2918.       rl_backward_word (-count);
  2919.       return;
  2920.     }
  2921.  
  2922.   while (count)
  2923.     {
  2924.       if (rl_point == rl_end)
  2925.     return;
  2926.  
  2927.       /* If we are not in a word, move forward until we are in one.
  2928.      Then, move forward until we hit a non-alphabetic character. */
  2929.       c = the_line[rl_point];
  2930.       if (!alphabetic (c))
  2931.     {
  2932.       while (++rl_point < rl_end)
  2933.         {
  2934.           c = the_line[rl_point];
  2935.           if (alphabetic (c)) break;
  2936.         }
  2937.     }
  2938.       if (rl_point == rl_end) return;
  2939.       while (++rl_point < rl_end)
  2940.     {
  2941.       c = the_line[rl_point];
  2942.       if (!alphabetic (c)) break;
  2943.     }
  2944.       --count;
  2945.     }
  2946. }
  2947.  
  2948. /* Move backward a word.  We do what Emacs does. */
  2949. rl_backward_word (count)
  2950.      int count;
  2951. {
  2952.   int c;
  2953.  
  2954.   if (count < 0)
  2955.     {
  2956.       rl_forward_word (-count);
  2957.       return;
  2958.     }
  2959.  
  2960.   while (count)
  2961.     {
  2962.       if (!rl_point)
  2963.     return;
  2964.  
  2965.       /* Like rl_forward_word (), except that we look at the characters
  2966.      just before point. */
  2967.  
  2968.       c = the_line[rl_point - 1];
  2969.       if (!alphabetic (c))
  2970.     {
  2971.       while (--rl_point)
  2972.         {
  2973.           c = the_line[rl_point - 1];
  2974.           if (alphabetic (c)) break;
  2975.         }
  2976.     }
  2977.  
  2978.       while (rl_point)
  2979.     {
  2980.       c = the_line[rl_point - 1];
  2981.       if (!alphabetic (c))
  2982.         break;
  2983.       else --rl_point;
  2984.     }
  2985.       --count;
  2986.     }
  2987. }
  2988.  
  2989. /* Clear the current line.  Numeric argument to C-l does this. */
  2990. rl_refresh_line ()
  2991. {
  2992.   int curr_line = last_c_pos / screenwidth;
  2993.   extern char *term_clreol;
  2994.  
  2995.   move_vert(curr_line);
  2996.   move_cursor_relative (0, the_line);   /* XXX is this right */
  2997.  
  2998. #ifdef __GO32__
  2999.   {
  3000.   int r, c, w;
  3001.   ScreenGetCursor(&r, &c);
  3002.   w = ScreenCols();
  3003.   memset(ScreenPrimary+r*w+c, 0, (w-c)*2);
  3004.   }
  3005. #else /* __GO32__ */
  3006.   if (term_clreol)
  3007.     tputs (term_clreol, 1, output_character_function);
  3008. #endif /* __GO32__/else */
  3009.  
  3010.   rl_forced_update_display ();
  3011.   rl_display_fixed = 1;
  3012. }
  3013.  
  3014. /* C-l typed to a line without quoting clears the screen, and then reprints
  3015.    the prompt and the current input line.  Given a numeric arg, redraw only
  3016.    the current line. */
  3017. rl_clear_screen ()
  3018. {
  3019.   extern char *term_clrpag;
  3020.  
  3021.   if (rl_explicit_arg)
  3022.     {
  3023.       rl_refresh_line ();
  3024.       return;
  3025.     }
  3026.  
  3027. #ifndef __GO32__
  3028.   if (term_clrpag)
  3029.     tputs (term_clrpag, 1, output_character_function);
  3030.   else
  3031. #endif /* !__GO32__ */
  3032.     crlf ();
  3033.  
  3034.   rl_forced_update_display ();
  3035.   rl_display_fixed = 1;
  3036. }
  3037.  
  3038. rl_arrow_keys (count, c)
  3039.      int count, c;
  3040. {
  3041.   int ch;
  3042.  
  3043.   ch = rl_read_key ();
  3044.  
  3045.   switch (to_upper (ch))
  3046.     {
  3047.     case 'A':
  3048.       rl_get_previous_history (count);
  3049.       break;
  3050.  
  3051.     case 'B':
  3052.       rl_get_next_history (count);
  3053.       break;
  3054.  
  3055.     case 'C':
  3056.       rl_forward (count);
  3057.       break;
  3058.  
  3059.     case 'D':
  3060.       rl_backward (count);
  3061.       break;
  3062.  
  3063.     default:
  3064.       ding ();
  3065.     }
  3066. }
  3067.  
  3068.  
  3069. /* **************************************************************** */
  3070. /*                                    */
  3071. /*            Text commands                    */
  3072. /*                                    */
  3073. /* **************************************************************** */
  3074.  
  3075. /* Insert the character C at the current location, moving point forward. */
  3076. rl_insert (count, c)
  3077.      int count, c;
  3078. {
  3079.   register int i;
  3080.   char *string;
  3081.  
  3082.   if (count <= 0)
  3083.     return;
  3084.  
  3085.   /* If we can optimize, then do it.  But don't let people crash
  3086.      readline because of extra large arguments. */
  3087.   if (count > 1 && count < 1024)
  3088.     {
  3089.       string = (char *)alloca (1 + count);
  3090.  
  3091.       for (i = 0; i < count; i++)
  3092.     string[i] = c;
  3093.  
  3094.       string[i] = '\0';
  3095.       rl_insert_text (string);
  3096.       return;
  3097.     }
  3098.  
  3099.   if (count > 1024)
  3100.     {
  3101.       int decreaser;
  3102.  
  3103.       string = (char *)alloca (1024 + 1);
  3104.  
  3105.       for (i = 0; i < 1024; i++)
  3106.     string[i] = c;
  3107.  
  3108.       while (count)
  3109.     {
  3110.       decreaser = (count > 1024 ? 1024 : count);
  3111.       string[decreaser] = '\0';
  3112.       rl_insert_text (string);
  3113.       count -= decreaser;
  3114.     }
  3115.       return;
  3116.     }
  3117.  
  3118.   /* We are inserting a single character.
  3119.      If there is pending input, then make a string of all of the
  3120.      pending characters that are bound to rl_insert, and insert
  3121.      them all. */
  3122.   if (any_typein)
  3123.     {
  3124.       int key = 0, t;
  3125.  
  3126.       i = 0;
  3127.       string = (char *)alloca (ibuffer_len + 1);
  3128.       string[i++] = c;
  3129.  
  3130.       while ((t = rl_get_char (&key)) &&
  3131.          (keymap[key].type == ISFUNC &&
  3132.           keymap[key].function == rl_insert))
  3133.     string[i++] = key;
  3134.  
  3135.       if (t)
  3136.     rl_unget_char (key);
  3137.  
  3138.       string[i] = '\0';
  3139.       rl_insert_text (string);
  3140.       return;
  3141.     }
  3142.   else
  3143.     {
  3144.       /* Inserting a single character. */
  3145.       string = (char *)alloca (2);
  3146.  
  3147.       string[1] = '\0';
  3148.       string[0] = c;
  3149.       rl_insert_text (string);
  3150.     }
  3151. }
  3152.  
  3153. /* Insert the next typed character verbatim. */
  3154. rl_quoted_insert (count)
  3155.      int count;
  3156. {
  3157.   int c = rl_read_key ();
  3158.   rl_insert (count, c);
  3159. }
  3160.  
  3161. /* Insert a tab character. */
  3162. rl_tab_insert (count)
  3163.      int count;
  3164. {
  3165.   rl_insert (count, '\t');
  3166. }
  3167.  
  3168. /* What to do when a NEWLINE is pressed.  We accept the whole line.
  3169.    KEY is the key that invoked this command.  I guess it could have
  3170.    meaning in the future. */
  3171. rl_newline (count, key)
  3172.      int count, key;
  3173. {
  3174.  
  3175.   rl_done = 1;
  3176.  
  3177. #if defined (VI_MODE)
  3178.   {
  3179.     extern int vi_doing_insert;
  3180.     if (vi_doing_insert)
  3181.       {
  3182.     rl_end_undo_group ();
  3183.     vi_doing_insert = 0;
  3184.       }
  3185.   }
  3186. #endif /* VI_MODE */
  3187.  
  3188.   if (readline_echoing_p)
  3189.     {
  3190.       move_vert (vis_botlin);
  3191.       vis_botlin = 0;
  3192.       crlf ();
  3193.       fflush (out_stream);
  3194.       rl_display_fixed++;
  3195.     }
  3196. }
  3197.  
  3198. rl_clean_up_for_exit ()
  3199. {
  3200.   if (readline_echoing_p)
  3201.     {
  3202.       move_vert (vis_botlin);
  3203.       vis_botlin = 0;
  3204.       fflush (out_stream);
  3205.       rl_restart_output ();
  3206.     }
  3207. }
  3208.  
  3209. /* What to do for some uppercase characters, like meta characters,
  3210.    and some characters appearing in emacs_ctlx_keymap.  This function
  3211.    is just a stub, you bind keys to it and the code in rl_dispatch ()
  3212.    is special cased. */
  3213. rl_do_lowercase_version (ignore1, ignore2)
  3214.      int ignore1, ignore2;
  3215. {
  3216. }
  3217.  
  3218. /* Rubout the character behind point. */
  3219. rl_rubout (count)
  3220.      int count;
  3221. {
  3222.   if (count < 0)
  3223.     {
  3224.       rl_delete (-count);
  3225.       return;
  3226.     }
  3227.  
  3228.   if (!rl_point)
  3229.     {
  3230.       ding ();
  3231.       return;
  3232.     }
  3233.  
  3234.   if (count > 1)
  3235.     {
  3236.       int orig_point = rl_point;
  3237.       rl_backward (count);
  3238.       rl_kill_text (orig_point, rl_point);
  3239.     }
  3240.   else
  3241.     {
  3242.       int c = the_line[--rl_point];
  3243.       rl_delete_text (rl_point, rl_point + 1);
  3244.  
  3245.       if (rl_point == rl_end && alphabetic (c) && last_c_pos)
  3246.     {
  3247.       backspace (1);
  3248.       putc (' ', out_stream);
  3249.       backspace (1);
  3250.       last_c_pos--;
  3251.       visible_line[last_c_pos] = '\0';
  3252.       rl_display_fixed++;
  3253.     }
  3254.     }
  3255. }
  3256.  
  3257. /* Delete the character under the cursor.  Given a numeric argument,
  3258.    kill that many characters instead. */
  3259. rl_delete (count, invoking_key)
  3260.      int count, invoking_key;
  3261. {
  3262.   if (count < 0)
  3263.     {
  3264.       rl_rubout (-count);
  3265.       return;
  3266.     }
  3267.  
  3268.   if (rl_point == rl_end)
  3269.     {
  3270.       ding ();
  3271.       return;
  3272.     }
  3273.  
  3274.   if (count > 1)
  3275.     {
  3276.       int orig_point = rl_point;
  3277.       rl_forward (count);
  3278.       rl_kill_text (orig_point, rl_point);
  3279.       rl_point = orig_point;
  3280.     }
  3281.   else
  3282.     rl_delete_text (rl_point, rl_point + 1);
  3283. }
  3284.  
  3285.  
  3286. /* **************************************************************** */
  3287. /*                                    */
  3288. /*            Kill commands                    */
  3289. /*                                    */
  3290. /* **************************************************************** */
  3291.  
  3292. /* The next two functions mimic unix line editing behaviour, except they
  3293.    save the deleted text on the kill ring.  This is safer than not saving
  3294.    it, and since we have a ring, nobody should get screwed. */
  3295.  
  3296. /* This does what C-w does in Unix.  We can't prevent people from
  3297.    using behaviour that they expect. */
  3298. rl_unix_word_rubout ()
  3299. {
  3300.   if (!rl_point) ding ();
  3301.   else {
  3302.     int orig_point = rl_point;
  3303.     while (rl_point && whitespace (the_line[rl_point - 1]))
  3304.       rl_point--;
  3305.     while (rl_point && !whitespace (the_line[rl_point - 1]))
  3306.       rl_point--;
  3307.     rl_kill_text (rl_point, orig_point);
  3308.   }
  3309. }
  3310.  
  3311. /* Here is C-u doing what Unix does.  You don't *have* to use these
  3312.    key-bindings.  We have a choice of killing the entire line, or
  3313.    killing from where we are to the start of the line.  We choose the
  3314.    latter, because if you are a Unix weenie, then you haven't backspaced
  3315.    into the line at all, and if you aren't, then you know what you are
  3316.    doing. */
  3317. rl_unix_line_discard ()
  3318. {
  3319.   if (!rl_point) ding ();
  3320.   else {
  3321.     rl_kill_text (rl_point, 0);
  3322.     rl_point = 0;
  3323.   }
  3324. }
  3325.  
  3326.  
  3327.  
  3328. /* **************************************************************** */
  3329. /*                                    */
  3330. /*            Commands For Typos                */
  3331. /*                                    */
  3332. /* **************************************************************** */
  3333.  
  3334. /* Random and interesting things in here.  */
  3335.  
  3336. /* **************************************************************** */
  3337. /*                                    */
  3338. /*            Changing Case                    */
  3339. /*                                    */
  3340. /* **************************************************************** */
  3341.  
  3342. /* The three kinds of things that we know how to do. */
  3343. #define UpCase 1
  3344. #define DownCase 2
  3345. #define CapCase 3
  3346.  
  3347. /* Uppercase the word at point. */
  3348. rl_upcase_word (count)
  3349.      int count;
  3350. {
  3351.   rl_change_case (count, UpCase);
  3352. }
  3353.  
  3354. /* Lowercase the word at point. */
  3355. rl_downcase_word (count)
  3356.      int count;
  3357. {
  3358.   rl_change_case (count, DownCase);
  3359. }
  3360.  
  3361. /* Upcase the first letter, downcase the rest. */
  3362. rl_capitalize_word (count)
  3363.      int count;
  3364. {
  3365.   rl_change_case (count, CapCase);
  3366. }
  3367.  
  3368. /* The meaty function.
  3369.    Change the case of COUNT words, performing OP on them.
  3370.    OP is one of UpCase, DownCase, or CapCase.
  3371.    If a negative argument is given, leave point where it started,
  3372.    otherwise, leave it where it moves to. */
  3373. rl_change_case (count, op)
  3374.      int count, op;
  3375. {
  3376.   register int start = rl_point, end;
  3377.   int state = 0;
  3378.  
  3379.   rl_forward_word (count);
  3380.   end = rl_point;
  3381.  
  3382.   if (count < 0)
  3383.     {
  3384.       int temp = start;
  3385.       start = end;
  3386.       end = temp;
  3387.     }
  3388.  
  3389.   /* We are going to modify some text, so let's prepare to undo it. */
  3390.   rl_modifying (start, end);
  3391.  
  3392.   for (; start < end; start++)
  3393.     {
  3394.       switch (op)
  3395.     {
  3396.     case UpCase:
  3397.       the_line[start] = to_upper (the_line[start]);
  3398.       break;
  3399.  
  3400.     case DownCase:
  3401.       the_line[start] = to_lower (the_line[start]);
  3402.       break;
  3403.  
  3404.     case CapCase:
  3405.       if (state == 0)
  3406.         {
  3407.           the_line[start] = to_upper (the_line[start]);
  3408.           state = 1;
  3409.         }
  3410.       else
  3411.         {
  3412.           the_line[start] = to_lower (the_line[start]);
  3413.         }
  3414.       if (!pure_alphabetic (the_line[start]))
  3415.         state = 0;
  3416.       break;
  3417.  
  3418.     default:
  3419.       abort ();
  3420.     }
  3421.     }
  3422.   rl_point = end;
  3423. }
  3424.  
  3425. /* **************************************************************** */
  3426. /*                                    */
  3427. /*            Transposition                    */
  3428. /*                                    */
  3429. /* **************************************************************** */
  3430.  
  3431. /* Transpose the words at point. */
  3432. rl_transpose_words (count)
  3433.      int count;
  3434. {
  3435.   char *word1, *word2;
  3436.   int w1_beg, w1_end, w2_beg, w2_end;
  3437.   int orig_point = rl_point;
  3438.  
  3439.   if (!count) return;
  3440.  
  3441.   /* Find the two words. */
  3442.   rl_forward_word (count);
  3443.   w2_end = rl_point;
  3444.   rl_backward_word (1);
  3445.   w2_beg = rl_point;
  3446.   rl_backward_word (count);
  3447.   w1_beg = rl_point;
  3448.   rl_forward_word (1);
  3449.   w1_end = rl_point;
  3450.  
  3451.   /* Do some check to make sure that there really are two words. */
  3452.   if ((w1_beg == w2_beg) || (w2_beg < w1_end))
  3453.     {
  3454.       ding ();
  3455.       rl_point = orig_point;
  3456.       return;
  3457.     }
  3458.  
  3459.   /* Get the text of the words. */
  3460.   word1 = rl_copy (w1_beg, w1_end);
  3461.   word2 = rl_copy (w2_beg, w2_end);
  3462.  
  3463.   /* We are about to do many insertions and deletions.  Remember them
  3464.      as one operation. */
  3465.   rl_begin_undo_group ();
  3466.  
  3467.   /* Do the stuff at word2 first, so that we don't have to worry
  3468.      about word1 moving. */
  3469.   rl_point = w2_beg;
  3470.   rl_delete_text (w2_beg, w2_end);
  3471.   rl_insert_text (word1);
  3472.  
  3473.   rl_point = w1_beg;
  3474.   rl_delete_text (w1_beg, w1_end);
  3475.   rl_insert_text (word2);
  3476.  
  3477.   /* This is exactly correct since the text before this point has not
  3478.      changed in length. */
  3479.   rl_point = w2_end;
  3480.  
  3481.   /* I think that does it. */
  3482.   rl_end_undo_group ();
  3483.   free (word1); free (word2);
  3484. }
  3485.  
  3486. /* Transpose the characters at point.  If point is at the end of the line,
  3487.    then transpose the characters before point. */
  3488. rl_transpose_chars (count)
  3489.      int count;
  3490. {
  3491.   if (!count)
  3492.     return;
  3493.  
  3494.   if (!rl_point || rl_end < 2) {
  3495.     ding ();
  3496.     return;
  3497.   }
  3498.  
  3499.   while (count)
  3500.     {
  3501.       if (rl_point == rl_end)
  3502.     {
  3503.       int t = the_line[rl_point - 1];
  3504.  
  3505.       the_line[rl_point - 1] = the_line[rl_point - 2];
  3506.       the_line[rl_point - 2] = t;
  3507.     }
  3508.       else
  3509.     {
  3510.       int t = the_line[rl_point];
  3511.  
  3512.       the_line[rl_point] = the_line[rl_point - 1];
  3513.       the_line[rl_point - 1] = t;
  3514.  
  3515.       if (count < 0 && rl_point)
  3516.         rl_point--;
  3517.       else
  3518.         rl_point++;
  3519.     }
  3520.  
  3521.       if (count < 0)
  3522.     count++;
  3523.       else
  3524.     count--;
  3525.     }
  3526. }
  3527.  
  3528.  
  3529. /* **************************************************************** */
  3530. /*                                    */
  3531. /*            Bogus Flow Control                  */
  3532. /*                                    */
  3533. /* **************************************************************** */
  3534.  
  3535. rl_restart_output (count, key)
  3536.      int count, key;
  3537. {
  3538.   int fildes = fileno (rl_outstream);
  3539. #if defined (TIOCSTART)
  3540. #if defined (apollo)
  3541.   ioctl (&fildes, TIOCSTART, 0);
  3542. #else
  3543.   ioctl (fildes, TIOCSTART, 0);
  3544. #endif /* apollo */
  3545.  
  3546. #else
  3547. #  if defined (TERMIOS_TTY_DRIVER)
  3548.         tcflow (fildes, TCOON);
  3549. #  else
  3550. #    if defined (TCXONC)
  3551.         ioctl (fildes, TCXONC, TCOON);
  3552. #    endif /* TCXONC */
  3553. #  endif /* !TERMIOS_TTY_DRIVER */
  3554. #endif /* TIOCSTART */
  3555. }
  3556.  
  3557. rl_stop_output (count, key)
  3558.      int count, key;
  3559. {
  3560.   int fildes = fileno (rl_instream);
  3561.  
  3562. #if defined (TIOCSTOP)
  3563. # if defined (apollo)
  3564.   ioctl (&fildes, TIOCSTOP, 0);
  3565. # else
  3566.   ioctl (fildes, TIOCSTOP, 0);
  3567. # endif /* apollo */
  3568. #else
  3569. # if defined (TERMIOS_TTY_DRIVER)
  3570.   tcflow (fildes, TCOOFF);
  3571. # else
  3572. #   if defined (TCXONC)
  3573.   ioctl (fildes, TCXONC, TCOON);
  3574. #   endif /* TCXONC */
  3575. # endif /* !TERMIOS_TTY_DRIVER */
  3576. #endif /* TIOCSTOP */
  3577. }
  3578.  
  3579. /* **************************************************************** */
  3580. /*                                    */
  3581. /*    Completion matching, from readline's point of view.        */
  3582. /*                                    */
  3583. /* **************************************************************** */
  3584.  
  3585. /* Pointer to the generator function for completion_matches ().
  3586.    NULL means to use filename_entry_function (), the default filename
  3587.    completer. */
  3588. Function *rl_completion_entry_function = (Function *)NULL;
  3589.  
  3590. /* Pointer to alternative function to create matches.
  3591.    Function is called with TEXT, START, and END.
  3592.    START and END are indices in RL_LINE_BUFFER saying what the boundaries
  3593.    of TEXT are.
  3594.    If this function exists and returns NULL then call the value of
  3595.    rl_completion_entry_function to try to match, otherwise use the
  3596.    array of strings returned. */
  3597. Function *rl_attempted_completion_function = (Function *)NULL;
  3598.  
  3599. /* Local variable states what happened during the last completion attempt. */
  3600. static int completion_changed_buffer = 0;
  3601.  
  3602. /* Complete the word at or before point.  You have supplied the function
  3603.    that does the initial simple matching selection algorithm (see
  3604.    completion_matches ()).  The default is to do filename completion. */
  3605.  
  3606. rl_complete (ignore, invoking_key)
  3607.      int ignore, invoking_key;
  3608. {
  3609.   if (rl_last_func == rl_complete && !completion_changed_buffer)
  3610.     rl_complete_internal ('?');
  3611.   else
  3612.     rl_complete_internal (TAB);
  3613. }
  3614.  
  3615. /* List the possible completions.  See description of rl_complete (). */
  3616. rl_possible_completions ()
  3617. {
  3618.   rl_complete_internal ('?');
  3619. }
  3620.  
  3621. /* The user must press "y" or "n". Non-zero return means "y" pressed. */
  3622. get_y_or_n ()
  3623. {
  3624.   int c;
  3625.  loop:
  3626.   c = rl_read_key ();
  3627.   if (c == 'y' || c == 'Y') return (1);
  3628.   if (c == 'n' || c == 'N') return (0);
  3629.   if (c == ABORT_CHAR) rl_abort ();
  3630.   ding (); goto loop;
  3631. }
  3632.  
  3633. /* Up to this many items will be displayed in response to a
  3634.    possible-completions call.  After that, we ask the user if
  3635.    she is sure she wants to see them all. */
  3636. int rl_completion_query_items = 100;
  3637.  
  3638. /* The basic list of characters that signal a break between words for the
  3639.    completer routine.  The contents of this variable is what breaks words
  3640.    in the shell, i.e. " \t\n\"\\'`@@$><=" */
  3641. char *rl_basic_word_break_characters = " \t\n\"\\'`@@$><=;|&{(";
  3642.  
  3643. /* The list of characters that signal a break between words for
  3644.    rl_complete_internal.  The default list is the contents of
  3645.    rl_basic_word_break_characters.  */
  3646. char *rl_completer_word_break_characters = (char *)NULL;
  3647.  
  3648. /* List of characters that are word break characters, but should be left
  3649.    in TEXT when it is passed to the completion function.  The shell uses
  3650.    this to help determine what kind of completing to do. */
  3651. char *rl_special_prefixes = (char *)NULL;
  3652.  
  3653. /* If non-zero, then disallow duplicates in the matches. */
  3654. int rl_ignore_completion_duplicates = 1;
  3655.  
  3656. /* Non-zero means that the results of the matches are to be treated
  3657.    as filenames.  This is ALWAYS zero on entry, and can only be changed
  3658.    within a completion entry finder function. */
  3659. int rl_filename_completion_desired = 0;
  3660.  
  3661. /* This function, if defined, is called by the completer when real
  3662.    filename completion is done, after all the matching names have been
  3663.    generated. It is passed a (char**) known as matches in the code below.
  3664.    It consists of a NULL-terminated array of pointers to potential
  3665.    matching strings.  The 1st element (matches[0]) is the maximal
  3666.    substring that is common to all matches. This function can re-arrange
  3667.    the list of matches as required, but all elements of the array must be
  3668.    free()'d if they are deleted. The main intent of this function is
  3669.    to implement FIGNORE a la SunOS csh. */
  3670. Function *rl_ignore_some_completions_function = (Function *)NULL;
  3671.  
  3672. /* Complete the word at or before point.
  3673.    WHAT_TO_DO says what to do with the completion.
  3674.    `?' means list the possible completions.
  3675.    TAB means do standard completion.
  3676.    `*' means insert all of the possible completions. */
  3677. rl_complete_internal (what_to_do)
  3678.      int what_to_do;
  3679. {
  3680.   char *filename_completion_function ();
  3681.   char **completion_matches (), **matches;
  3682.   Function *our_func;
  3683.   int start, end, delimiter = 0;
  3684.   char *text, *saved_line_buffer;
  3685.  
  3686.   if (the_line)
  3687.     saved_line_buffer = savestring (the_line);
  3688.   else
  3689.     saved_line_buffer = (char *)NULL;
  3690.  
  3691.   if (rl_completion_entry_function)
  3692.     our_func = rl_completion_entry_function;
  3693.   else
  3694.     our_func = (int (*)())filename_completion_function;
  3695.  
  3696.   /* Only the completion entry function can change this. */
  3697.   rl_filename_completion_desired = 0;
  3698.  
  3699.   /* We now look backwards for the start of a filename/variable word. */
  3700.   end = rl_point;
  3701.  
  3702.   if (rl_point)
  3703.     {
  3704.       while (--rl_point &&
  3705.          !rindex (rl_completer_word_break_characters, the_line[rl_point]));
  3706.  
  3707.       /* If we are at a word break, then advance past it. */
  3708.       if (rindex (rl_completer_word_break_characters, the_line[rl_point]))
  3709.     {
  3710.       /* If the character that caused the word break was a quoting
  3711.          character, then remember it as the delimiter. */
  3712.       if (rindex ("\"'", the_line[rl_point]) && (end - rl_point) > 1)
  3713.         delimiter = the_line[rl_point];
  3714.  
  3715.       /* If the character isn't needed to determine something special
  3716.          about what kind of completion to perform, then advance past it. */
  3717.  
  3718.       if (!rl_special_prefixes ||
  3719.           !rindex (rl_special_prefixes, the_line[rl_point]))
  3720.         rl_point++;
  3721.     }
  3722.     }
  3723.  
  3724.   start = rl_point;
  3725.   rl_point = end;
  3726.   text = rl_copy (start, end);
  3727.  
  3728.   /* If the user wants to TRY to complete, but then wants to give
  3729.      up and use the default completion function, they set the
  3730.      variable rl_attempted_completion_function. */
  3731.   if (rl_attempted_completion_function)
  3732.     {
  3733.       matches =
  3734.     (char **)(*rl_attempted_completion_function) (text, start, end);
  3735.  
  3736.       if (matches)
  3737.     {
  3738.       our_func = (Function *)NULL;
  3739.       goto after_usual_completion;
  3740.     }
  3741.     }
  3742.  
  3743.   matches = completion_matches (text, our_func);
  3744.  
  3745.  after_usual_completion:
  3746.   free (text);
  3747.  
  3748.   if (!matches)
  3749.     ding ();
  3750.   else
  3751.     {
  3752.       register int i;
  3753.  
  3754.     some_matches:
  3755.  
  3756.       /* It seems to me that in all the cases we handle we would like
  3757.      to ignore duplicate possiblilities.  Scan for the text to
  3758.      insert being identical to the other completions. */
  3759.       if (rl_ignore_completion_duplicates)
  3760.     {
  3761.       char *lowest_common;
  3762.       int j, newlen = 0;
  3763.  
  3764.       /* Sort the items. */
  3765.       /* It is safe to sort this array, because the lowest common
  3766.          denominator found in matches[0] will remain in place. */
  3767.       for (i = 0; matches[i]; i++);
  3768.       qsort (matches, i, sizeof (char *), compare_strings);
  3769.  
  3770.       /* Remember the lowest common denominator for it may be unique. */
  3771.       lowest_common = savestring (matches[0]);
  3772.  
  3773.       for (i = 0; matches[i + 1]; i++)
  3774.         {
  3775.           if (strcmp (matches[i], matches[i + 1]) == 0)
  3776.         {
  3777.           free (matches[i]);
  3778.           matches[i] = (char *)-1;
  3779.         }
  3780.           else
  3781.         newlen++;
  3782.         }
  3783.  
  3784.       /* We have marked all the dead slots with (char *)-1.
  3785.          Copy all the non-dead entries into a new array. */
  3786.       {
  3787.         char **temp_array =
  3788.           (char **)malloc ((3 + newlen) * sizeof (char *));
  3789.  
  3790.         for (i = 1, j = 1; matches[i]; i++)
  3791.           {
  3792.         if (matches[i] != (char *)-1)
  3793.           temp_array[j++] = matches[i];
  3794.           }
  3795.  
  3796.         temp_array[j] = (char *)NULL;
  3797.  
  3798.         if (matches[0] != (char *)-1)
  3799.           free (matches[0]);
  3800.  
  3801.         free (matches);
  3802.  
  3803.         matches = temp_array;
  3804.       }
  3805.  
  3806.       /* Place the lowest common denominator back in [0]. */
  3807.       matches[0] = lowest_common;
  3808.  
  3809.       /* If there is one string left, and it is identical to the
  3810.          lowest common denominator, then the LCD is the string to
  3811.          insert. */
  3812.       if (j == 2 && strcmp (matches[0], matches[1]) == 0)
  3813.         {
  3814.           free (matches[1]);
  3815.           matches[1] = (char *)NULL;
  3816.         }
  3817.     }
  3818.  
  3819.       switch (what_to_do)
  3820.     {
  3821.     case TAB:
  3822.       /* If we are matching filenames, then here is our chance to
  3823.          do clever processing by re-examining the list.  Call the
  3824.          ignore function with the array as a parameter.  It can
  3825.          munge the array, deleting matches as it desires. */
  3826.       if (rl_ignore_some_completions_function &&
  3827.           our_func == (int (*)())filename_completion_function)
  3828.         (void)(*rl_ignore_some_completions_function)(matches);
  3829.  
  3830.       if (matches[0])
  3831.         {
  3832.           rl_delete_text (start, rl_point);
  3833.           rl_point = start;
  3834.           rl_insert_text (matches[0]);
  3835.         }
  3836.  
  3837.       /* If there are more matches, ring the bell to indicate.
  3838.          If this was the only match, and we are hacking files,
  3839.          check the file to see if it was a directory.  If so,
  3840.          add a '/' to the name.  If not, and we are at the end
  3841.          of the line, then add a space. */
  3842.       if (matches[1])
  3843.         {
  3844.           ding ();        /* There are other matches remaining. */
  3845.         }
  3846.       else
  3847.         {
  3848.           char temp_string[2];
  3849.  
  3850.           temp_string[0] = delimiter ? delimiter : ' ';
  3851.           temp_string[1] = '\0';
  3852.  
  3853.           if (rl_filename_completion_desired)
  3854.         {
  3855.           struct stat finfo;
  3856.           char *filename = tilde_expand (matches[0]);
  3857.  
  3858.           if ((stat (filename, &finfo) == 0) &&
  3859.               S_ISDIR (finfo.st_mode))
  3860.             {
  3861.               if (the_line[rl_point] != '/')
  3862.             rl_insert_text ("/");
  3863.             }
  3864.           else
  3865.             {
  3866.               if (rl_point == rl_end)
  3867.             rl_insert_text (temp_string);
  3868.             }
  3869.           free (filename);
  3870.         }
  3871.           else
  3872.         {
  3873.           if (rl_point == rl_end)
  3874.             rl_insert_text (temp_string);
  3875.         }
  3876.         }
  3877.       break;
  3878.  
  3879.     case '*':
  3880.       {
  3881.         int i = 1;
  3882.  
  3883.         rl_delete_text (start, rl_point);
  3884.         rl_point = start;
  3885.         rl_begin_undo_group ();
  3886.         if (matches[1])
  3887.           {
  3888.         while (matches[i])
  3889.           {
  3890.             rl_insert_text (matches[i++]);
  3891.             rl_insert_text (" ");
  3892.           }
  3893.           }
  3894.         else
  3895.           {
  3896.         rl_insert_text (matches[0]);
  3897.         rl_insert_text (" ");
  3898.           }
  3899.         rl_end_undo_group ();
  3900.       }
  3901.       break;
  3902.  
  3903.     case '?':
  3904.       {
  3905.         int len, count, limit, max = 0;
  3906.         int j, k, l;
  3907.  
  3908.         /* Handle simple case first.  What if there is only one answer? */
  3909.         if (!matches[1])
  3910.           {
  3911.         char *temp;
  3912.  
  3913.         if (rl_filename_completion_desired)
  3914.           temp = rindex (matches[0], '/');
  3915.         else
  3916.           temp = (char *)NULL;
  3917.  
  3918.         if (!temp)
  3919.           temp = matches[0];
  3920.         else
  3921.           temp++;
  3922.  
  3923.         crlf ();
  3924.         fprintf (out_stream, "%s", temp);
  3925.         crlf ();
  3926.         goto restart;
  3927.           }
  3928.  
  3929.         /* There is more than one answer.  Find out how many there are,
  3930.            and find out what the maximum printed length of a single entry
  3931.            is. */
  3932.         for (i = 1; matches[i]; i++)
  3933.           {
  3934.         char *temp = (char *)NULL;
  3935.  
  3936.         /* If we are hacking filenames, then only count the characters
  3937.            after the last slash in the pathname. */
  3938.         if (rl_filename_completion_desired)
  3939.           temp = rindex (matches[i], '/');
  3940.         else
  3941.           temp = (char *)NULL;
  3942.  
  3943.         if (!temp)
  3944.           temp = matches[i];
  3945.         else
  3946.           temp++;
  3947.  
  3948.         if (strlen (temp) > max)
  3949.           max = strlen (temp);
  3950.           }
  3951.  
  3952.         len = i;
  3953.  
  3954.         /* If there are many items, then ask the user if she
  3955.            really wants to see them all. */
  3956.         if (len >= rl_completion_query_items)
  3957.           {
  3958.         crlf ();
  3959.         fprintf (out_stream,
  3960.              "There are %d possibilities.  Do you really", len);
  3961.         crlf ();
  3962.         fprintf (out_stream, "wish to see them all? (y or n)");
  3963.         fflush (out_stream);
  3964.         if (!get_y_or_n ())
  3965.           {
  3966.             crlf ();
  3967.             goto restart;
  3968.           }
  3969.           }
  3970.         /* How many items of MAX length can we fit in the screen window? */
  3971.         max += 2;
  3972.         limit = screenwidth / max;
  3973.         if (limit != 1 && (limit * max == screenwidth))
  3974.           limit--;
  3975.  
  3976.         /* Avoid a possible floating exception.  If max > screenwidth,
  3977.            limit will be 0 and a divide-by-zero fault will result. */
  3978.         if (limit == 0)
  3979.           limit = 1;
  3980.  
  3981.         /* How many iterations of the printing loop? */
  3982.         count = (len + (limit - 1)) / limit;
  3983.  
  3984.         /* Watch out for special case.  If LEN is less than LIMIT, then
  3985.            just do the inner printing loop. */
  3986.         if (len < limit) count = 1;
  3987.  
  3988.         /* Sort the items if they are not already sorted. */
  3989.         if (!rl_ignore_completion_duplicates)
  3990.           qsort (matches, len, sizeof (char *), compare_strings);
  3991.  
  3992.         /* Print the sorted items, up-and-down alphabetically, like
  3993.            ls might. */
  3994.         crlf ();
  3995.  
  3996.         for (i = 1; i < count + 1; i++)
  3997.           {
  3998.         for (j = 0, l = i; j < limit; j++)
  3999.           {
  4000.             if (l > len || !matches[l])
  4001.               {
  4002.             break;
  4003.               }
  4004.             else
  4005.               {
  4006.             char *temp = (char *)NULL;
  4007.  
  4008.             if (rl_filename_completion_desired)
  4009.               temp = rindex (matches[l], '/');
  4010.             else
  4011.               temp = (char *)NULL;
  4012.  
  4013.             if (!temp)
  4014.               temp = matches[l];
  4015.             else
  4016.               temp++;
  4017.  
  4018.             fprintf (out_stream, "%s", temp);
  4019.             for (k = 0; k < max - strlen (temp); k++)
  4020.               putc (' ', out_stream);
  4021.               }
  4022.             l += count;
  4023.           }
  4024.         crlf ();
  4025.           }
  4026.       restart:
  4027.  
  4028.         rl_on_new_line ();
  4029.       }
  4030.       break;
  4031.  
  4032.     default:
  4033.       abort ();
  4034.     }
  4035.  
  4036.       for (i = 0; matches[i]; i++)
  4037.     free (matches[i]);
  4038.       free (matches);
  4039.     }
  4040.  
  4041.   /* Check to see if the line has changed through all of this manipulation. */
  4042.   if (saved_line_buffer)
  4043.     {
  4044.       if (strcmp (the_line, saved_line_buffer) != 0)
  4045.     completion_changed_buffer = 1;
  4046.       else
  4047.     completion_changed_buffer = 0;
  4048.  
  4049.       free (saved_line_buffer);
  4050.     }
  4051. }
  4052.  
  4053. /* Stupid comparison routine for qsort () ing strings. */
  4054. static int
  4055. compare_strings (s1, s2)
  4056.   char **s1, **s2;
  4057. {
  4058.   return (strcmp (*s1, *s2));
  4059. }
  4060.  
  4061. /* A completion function for usernames.
  4062.    TEXT contains a partial username preceded by a random
  4063.    character (usually `~').  */
  4064. char *
  4065. username_completion_function (text, state)
  4066.      int state;
  4067.      char *text;
  4068. {
  4069. #ifdef __GO32__
  4070.   return (char *)NULL;
  4071. #else /* !__GO32__ */
  4072.   static char *username = (char *)NULL;
  4073.   static struct passwd *entry;
  4074.   static int namelen, first_char, first_char_loc;
  4075.  
  4076.   if (!state)
  4077.     {
  4078.       if (username)
  4079.     free (username);
  4080.  
  4081.       first_char = *text;
  4082.  
  4083.       if (first_char == '~')
  4084.     first_char_loc = 1;
  4085.       else
  4086.     first_char_loc = 0;
  4087.  
  4088.       username = savestring (&text[first_char_loc]);
  4089.       namelen = strlen (username);
  4090.       setpwent ();
  4091.     }
  4092.  
  4093.   while (entry = getpwent ())
  4094.     {
  4095.       if (strncmp (username, entry->pw_name, namelen) == 0)
  4096.     break;
  4097.     }
  4098.  
  4099.   if (!entry)
  4100.     {
  4101.       endpwent ();
  4102.       return ((char *)NULL);
  4103.     }
  4104.   else
  4105.     {
  4106.       char *value = (char *)xmalloc (2 + strlen (entry->pw_name));
  4107.  
  4108.       *value = *text;
  4109.  
  4110.       strcpy (value + first_char_loc, entry->pw_name);
  4111.  
  4112.       if (first_char == '~')
  4113.     rl_filename_completion_desired = 1;
  4114.  
  4115.       return (value);
  4116.     }
  4117. #endif /* !__GO32__ */
  4118. }
  4119.  
  4120. /* **************************************************************** */
  4121. /*                                    */
  4122. /*            Undo, and Undoing                */
  4123. /*                                    */
  4124. /* **************************************************************** */
  4125.  
  4126. /* Non-zero tells rl_delete_text and rl_insert_text to not add to
  4127.    the undo list. */
  4128. int doing_an_undo = 0;
  4129.  
  4130. /* The current undo list for THE_LINE. */
  4131. UNDO_LIST *rl_undo_list = (UNDO_LIST *)NULL;
  4132.  
  4133. /* Remember how to undo something.  Concatenate some undos if that
  4134.    seems right. */
  4135. rl_add_undo (what, start, end, text)
  4136.      enum undo_code what;
  4137.      int start, end;
  4138.      char *text;
  4139. {
  4140.   UNDO_LIST *temp = (UNDO_LIST *)xmalloc (sizeof (UNDO_LIST));
  4141.   temp->what = what;
  4142.   temp->start = start;
  4143.   temp->end = end;
  4144.   temp->text = text;
  4145.   temp->next = rl_undo_list;
  4146.   rl_undo_list = temp;
  4147. }
  4148.  
  4149. /* Free the existing undo list. */
  4150. free_undo_list ()
  4151. {
  4152.   while (rl_undo_list) {
  4153.     UNDO_LIST *release = rl_undo_list;
  4154.     rl_undo_list = rl_undo_list->next;
  4155.  
  4156.     if (release->what == UNDO_DELETE)
  4157.       free (release->text);
  4158.  
  4159.     free (release);
  4160.   }
  4161. }
  4162.  
  4163. /* Undo the next thing in the list.  Return 0 if there
  4164.    is nothing to undo, or non-zero if there was. */
  4165. int
  4166. rl_do_undo ()
  4167. {
  4168.   UNDO_LIST *release;
  4169.   int waiting_for_begin = 0;
  4170.  
  4171. undo_thing:
  4172.   if (!rl_undo_list)
  4173.     return (0);
  4174.  
  4175.   doing_an_undo = 1;
  4176.  
  4177.   switch (rl_undo_list->what) {
  4178.  
  4179.     /* Undoing deletes means inserting some text. */
  4180.   case UNDO_DELETE:
  4181.     rl_point = rl_undo_list->start;
  4182.     rl_insert_text (rl_undo_list->text);
  4183.     free (rl_undo_list->text);
  4184.     break;
  4185.  
  4186.     /* Undoing inserts means deleting some text. */
  4187.   case UNDO_INSERT:
  4188.     rl_delete_text (rl_undo_list->start, rl_undo_list->end);
  4189.     rl_point = rl_undo_list->start;
  4190.     break;
  4191.  
  4192.     /* Undoing an END means undoing everything 'til we get to
  4193.        a BEGIN. */
  4194.   case UNDO_END:
  4195.     waiting_for_begin++;
  4196.     break;
  4197.  
  4198.     /* Undoing a BEGIN means that we are done with this group. */
  4199.   case UNDO_BEGIN:
  4200.     if (waiting_for_begin)
  4201.       waiting_for_begin--;
  4202.     else
  4203.       abort ();
  4204.     break;
  4205.   }
  4206.  
  4207.   doing_an_undo = 0;
  4208.  
  4209.   release = rl_undo_list;
  4210.   rl_undo_list = rl_undo_list->next;
  4211.   free (release);
  4212.  
  4213.   if (waiting_for_begin)
  4214.     goto undo_thing;
  4215.  
  4216.   return (1);
  4217. }
  4218.  
  4219. /* Begin a group.  Subsequent undos are undone as an atomic operation. */
  4220. rl_begin_undo_group ()
  4221. {
  4222.   rl_add_undo (UNDO_BEGIN, 0, 0, 0);
  4223. }
  4224.  
  4225. /* End an undo group started with rl_begin_undo_group (). */
  4226. rl_end_undo_group ()
  4227. {
  4228.   rl_add_undo (UNDO_END, 0, 0, 0);
  4229. }
  4230.  
  4231. /* Save an undo entry for the text from START to END. */
  4232. rl_modifying (start, end)
  4233.      int start, end;
  4234. {
  4235.   if (start > end)
  4236.     {
  4237.       int t = start;
  4238.       start = end;
  4239.       end = t;
  4240.     }
  4241.  
  4242.   if (start != end)
  4243.     {
  4244.       char *temp = rl_copy (start, end);
  4245.       rl_begin_undo_group ();
  4246.       rl_add_undo (UNDO_DELETE, start, end, temp);
  4247.       rl_add_undo (UNDO_INSERT, start, end, (char *)NULL);
  4248.       rl_end_undo_group ();
  4249.     }
  4250. }
  4251.  
  4252. /* Revert the current line to its previous state. */
  4253. rl_revert_line ()
  4254. {
  4255.   if (!rl_undo_list) ding ();
  4256.   else {
  4257.     while (rl_undo_list)
  4258.       rl_do_undo ();
  4259.   }
  4260. }
  4261.  
  4262. /* Do some undoing of things that were done. */
  4263. rl_undo_command (count)
  4264. {
  4265.   if (count < 0) return;    /* Nothing to do. */
  4266.  
  4267.   while (count)
  4268.     {
  4269.       if (rl_do_undo ())
  4270.     {
  4271.       count--;
  4272.     }
  4273.       else
  4274.     {
  4275.       ding ();
  4276.       break;
  4277.     }
  4278.     }
  4279. }
  4280.  
  4281. /* **************************************************************** */
  4282. /*                                    */
  4283. /*            History Utilities                */
  4284. /*                                    */
  4285. /* **************************************************************** */
  4286.  
  4287. /* We already have a history library, and that is what we use to control
  4288.    the history features of readline.  However, this is our local interface
  4289.    to the history mechanism. */
  4290.  
  4291. /* While we are editing the history, this is the saved
  4292.    version of the original line. */
  4293. HIST_ENTRY *saved_line_for_history = (HIST_ENTRY *)NULL;
  4294.  
  4295. /* Set the history pointer back to the last entry in the history. */
  4296. start_using_history ()
  4297. {
  4298.   using_history ();
  4299.   if (saved_line_for_history)
  4300.     free_history_entry (saved_line_for_history);
  4301.  
  4302.   saved_line_for_history = (HIST_ENTRY *)NULL;
  4303. }
  4304.  
  4305. /* Free the contents (and containing structure) of a HIST_ENTRY. */
  4306. free_history_entry (entry)
  4307.      HIST_ENTRY *entry;
  4308. {
  4309.   if (!entry) return;
  4310.   if (entry->line)
  4311.     free (entry->line);
  4312.   free (entry);
  4313. }
  4314.  
  4315. /* Perhaps put back the current line if it has changed. */
  4316. maybe_replace_line ()
  4317. {
  4318.   HIST_ENTRY *temp = current_history ();
  4319.  
  4320.   /* If the current line has changed, save the changes. */
  4321.   if (temp && ((UNDO_LIST *)(temp->data) != rl_undo_list))
  4322.     {
  4323.       temp = replace_history_entry (where_history (), the_line, rl_undo_list);
  4324.       free (temp->line);
  4325.       free (temp);
  4326.     }
  4327. }
  4328.  
  4329. /* Put back the saved_line_for_history if there is one. */
  4330. maybe_unsave_line ()
  4331. {
  4332.   if (saved_line_for_history)
  4333.     {
  4334.       int line_len;
  4335.  
  4336.       line_len = strlen (saved_line_for_history->line);
  4337.  
  4338.       if (line_len >= rl_line_buffer_len)
  4339.     rl_extend_line_buffer (line_len);
  4340.  
  4341.       strcpy (the_line, saved_line_for_history->line);
  4342.       rl_undo_list = (UNDO_LIST *)saved_line_for_history->data;
  4343.       free_history_entry (saved_line_for_history);
  4344.       saved_line_for_history = (HIST_ENTRY *)NULL;
  4345.       rl_end = rl_point = strlen (the_line);
  4346.     }
  4347.   else
  4348.     ding ();
  4349. }
  4350.  
  4351. /* Save the current line in saved_line_for_history. */
  4352. maybe_save_line ()
  4353. {
  4354.   if (!saved_line_for_history)
  4355.     {
  4356.       saved_line_for_history = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
  4357.       saved_line_for_history->line = savestring (the_line);
  4358.       saved_line_for_history->data = (char *)rl_undo_list;
  4359.     }
  4360. }
  4361.  
  4362. /* **************************************************************** */
  4363. /*                                    */
  4364. /*            History Commands                */
  4365. /*                                    */
  4366. /* **************************************************************** */
  4367.  
  4368. /* Meta-< goes to the start of the history. */
  4369. rl_beginning_of_history ()
  4370. {
  4371.   rl_get_previous_history (1 + where_history ());
  4372. }
  4373.  
  4374. /* Meta-> goes to the end of the history.  (The current line). */
  4375. rl_end_of_history ()
  4376. {
  4377.   maybe_replace_line ();
  4378.   using_history ();
  4379.   maybe_unsave_line ();
  4380. }
  4381.  
  4382. /* Move down to the next history line. */
  4383. rl_get_next_history (count)
  4384.      int count;
  4385. {
  4386.   HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
  4387.  
  4388.   if (count < 0)
  4389.     {
  4390.       rl_get_previous_history (-count);
  4391.       return;
  4392.     }
  4393.  
  4394.   if (!count)
  4395.     return;
  4396.  
  4397.   maybe_replace_line ();
  4398.  
  4399.   while (count)
  4400.     {
  4401.       temp = next_history ();
  4402.       if (!temp)
  4403.     break;
  4404.       --count;
  4405.     }
  4406.  
  4407.   if (!temp)
  4408.     maybe_unsave_line ();
  4409.   else
  4410.     {
  4411.       int line_len;
  4412.  
  4413.       line_len = strlen (temp->line);
  4414.  
  4415.       if (line_len >= rl_line_buffer_len)
  4416.     rl_extend_line_buffer (line_len);
  4417.  
  4418.       strcpy (the_line, temp->line);
  4419.       rl_undo_list = (UNDO_LIST *)temp->data;
  4420.       rl_end = rl_point = strlen (the_line);
  4421. #if defined (VI_MODE)
  4422.       if (rl_editing_mode == vi_mode)
  4423.     rl_point = 0;
  4424. #endif /* VI_MODE */
  4425.     }
  4426. }
  4427.  
  4428. /* Get the previous item out of our interactive history, making it the current
  4429.    line.  If there is no previous history, just ding. */
  4430. rl_get_previous_history (count)
  4431.      int count;
  4432. {
  4433.   HIST_ENTRY *old_temp = (HIST_ENTRY *)NULL;
  4434.   HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
  4435.  
  4436.   if (count < 0)
  4437.     {
  4438.       rl_get_next_history (-count);
  4439.       return;
  4440.     }
  4441.  
  4442.   if (!count)
  4443.     return;
  4444.  
  4445.   /* If we don't have a line saved, then save this one. */
  4446.   maybe_save_line ();
  4447.  
  4448.   /* If the current line has changed, save the changes. */
  4449.   maybe_replace_line ();
  4450.  
  4451.   while (count)
  4452.     {
  4453.       temp = previous_history ();
  4454.       if (!temp)
  4455.     break;
  4456.       else
  4457.     old_temp = temp;
  4458.       --count;
  4459.     }
  4460.  
  4461.   /* If there was a large argument, and we moved back to the start of the
  4462.      history, that is not an error.  So use the last value found. */
  4463.   if (!temp && old_temp)
  4464.     temp = old_temp;
  4465.  
  4466.   if (!temp)
  4467.     ding ();
  4468.   else
  4469.     {
  4470.       int line_len;
  4471.  
  4472.       line_len = strlen (temp->line);
  4473.  
  4474.       if (line_len >= rl_line_buffer_len)
  4475.     rl_extend_line_buffer (line_len);
  4476.  
  4477.       strcpy (the_line, temp->line);
  4478.       rl_undo_list = (UNDO_LIST *)temp->data;
  4479.       rl_end = rl_point = line_len;
  4480.  
  4481. #if defined (VI_MODE)
  4482.       if (rl_editing_mode == vi_mode)
  4483.     rl_point = 0;
  4484. #endif /* VI_MODE */
  4485.     }
  4486. }
  4487.  
  4488.  
  4489. /* **************************************************************** */
  4490. /*                                    */
  4491. /*            I-Search and Searching                */
  4492. /*                                    */
  4493. /* **************************************************************** */
  4494.  
  4495. /* Search backwards through the history looking for a string which is typed
  4496.    interactively.  Start with the current line. */
  4497. rl_reverse_search_history (sign, key)
  4498.      int sign;
  4499.      int key;
  4500. {
  4501.   rl_search_history (-sign, key);
  4502. }
  4503.  
  4504. /* Search forwards through the history looking for a string which is typed
  4505.    interactively.  Start with the current line. */
  4506. rl_forward_search_history (sign, key)
  4507.      int sign;
  4508.      int key;
  4509. {
  4510.   rl_search_history (sign, key);
  4511. }
  4512.  
  4513. /* Display the current state of the search in the echo-area.
  4514.    SEARCH_STRING contains the string that is being searched for,
  4515.    DIRECTION is zero for forward, or 1 for reverse,
  4516.    WHERE is the history list number of the current line.  If it is
  4517.    -1, then this line is the starting one. */
  4518. rl_display_search (search_string, reverse_p, where)
  4519.      char *search_string;
  4520.      int reverse_p, where;
  4521. {
  4522.   char *message = (char *)NULL;
  4523.  
  4524.   message =
  4525.     (char *)alloca (1 + (search_string ? strlen (search_string) : 0) + 30);
  4526.  
  4527.   *message = '\0';
  4528.  
  4529. #if defined (NOTDEF)
  4530.   if (where != -1)
  4531.     sprintf (message, "[%d]", where + history_base);
  4532. #endif /* NOTDEF */
  4533.  
  4534.   strcat (message, "(");
  4535.  
  4536.   if (reverse_p)
  4537.     strcat (message, "reverse-");
  4538.  
  4539.   strcat (message, "i-search)`");
  4540.  
  4541.   if (search_string)
  4542.     strcat (message, search_string);
  4543.  
  4544.   strcat (message, "': ");
  4545.   rl_message (message, 0, 0);
  4546.   rl_redisplay ();
  4547. }
  4548.  
  4549. /* Search through the history looking for an interactively typed string.
  4550.    This is analogous to i-search.  We start the search in the current line.
  4551.    DIRECTION is which direction to search; >= 0 means forward, < 0 means
  4552.    backwards. */
  4553. rl_search_history (direction, invoking_key)
  4554.      int direction;
  4555.      int invoking_key;
  4556. {
  4557.   /* The string that the user types in to search for. */
  4558.   char *search_string = (char *)alloca (128);
  4559.  
  4560.   /* The current length of SEARCH_STRING. */
  4561.   int search_string_index;
  4562.  
  4563.   /* The list of lines to search through. */
  4564.   char **lines;
  4565.  
  4566.   /* The length of LINES. */
  4567.   int hlen;
  4568.  
  4569.   /* Where we get LINES from. */
  4570.   HIST_ENTRY **hlist = history_list ();
  4571.  
  4572.   register int i = 0;
  4573.   int orig_point = rl_point;
  4574.   int orig_line = where_history ();
  4575.   int last_found_line = orig_line;
  4576.   int c, done = 0;
  4577.  
  4578.   /* The line currently being searched. */
  4579.   char *sline;
  4580.  
  4581.   /* Offset in that line. */
  4582.   int index;
  4583.  
  4584.   /* Non-zero if we are doing a reverse search. */
  4585.   int reverse = (direction < 0);
  4586.  
  4587.   /* Create an arrary of pointers to the lines that we want to search. */
  4588.   maybe_replace_line ();
  4589.   if (hlist)
  4590.     for (i = 0; hlist[i]; i++);
  4591.  
  4592.   /* Allocate space for this many lines, +1 for the current input line,
  4593.      and remember those lines. */
  4594.   lines = (char **)alloca ((1 + (hlen = i)) * sizeof (char *));
  4595.   for (i = 0; i < hlen; i++)
  4596.     lines[i] = hlist[i]->line;
  4597.  
  4598.   if (saved_line_for_history)
  4599.     lines[i] = saved_line_for_history->line;
  4600.   else
  4601.     /* So I have to type it in this way instead. */
  4602.     {
  4603.       char *alloced_line;
  4604.  
  4605.       /* Keep that mips alloca happy. */
  4606.       alloced_line = (char *)alloca (1 + strlen (the_line));
  4607.       lines[i] = alloced_line;
  4608.       strcpy (lines[i], &the_line[0]);
  4609.     }
  4610.  
  4611.   hlen++;
  4612.  
  4613.   /* The line where we start the search. */
  4614.   i = orig_line;
  4615.  
  4616.   /* Initialize search parameters. */
  4617.   *search_string = '\0';
  4618.   search_string_index = 0;
  4619.  
  4620.   /* Normalize DIRECTION into 1 or -1. */
  4621.   if (direction >= 0)
  4622.     direction = 1;
  4623.   else
  4624.     direction = -1;
  4625.  
  4626.   rl_display_search (search_string, reverse, -1);
  4627.  
  4628.   sline = the_line;
  4629.   index = rl_point;
  4630.  
  4631.   while (!done)
  4632.     {
  4633.       c = rl_read_key ();
  4634.  
  4635.       /* Hack C to Do What I Mean. */
  4636.       {
  4637.     Function *f = (Function *)NULL;
  4638.  
  4639.     if (keymap[c].type == ISFUNC)
  4640.       {
  4641.         f = keymap[c].function;
  4642.  
  4643.         if (f == rl_reverse_search_history)
  4644.           c = reverse ? -1 : -2;
  4645.         else if (f == rl_forward_search_history)
  4646.           c =  !reverse ? -1 : -2;
  4647.       }
  4648.       }
  4649.  
  4650.       switch (c)
  4651.     {
  4652.     case ESC:
  4653.       done = 1;
  4654.       continue;
  4655.  
  4656.       /* case invoking_key: */
  4657.     case -1:
  4658.       goto search_again;
  4659.  
  4660.       /* switch directions */
  4661.     case -2:
  4662.       direction = -direction;
  4663.       reverse = (direction < 0);
  4664.  
  4665.       goto do_search;
  4666.  
  4667.     case CTRL ('G'):
  4668.       strcpy (the_line, lines[orig_line]);
  4669.       rl_point = orig_point;
  4670.       rl_end = strlen (the_line);
  4671.       rl_clear_message ();
  4672.       return;
  4673.  
  4674.     default:
  4675.       if (c < 32 || c > 126)
  4676.         {
  4677.           rl_execute_next (c);
  4678.           done = 1;
  4679.           continue;
  4680.         }
  4681.       else
  4682.         {
  4683.           search_string[search_string_index++] = c;
  4684.           search_string[search_string_index] = '\0';
  4685.           goto do_search;
  4686.  
  4687.         search_again:
  4688.  
  4689.           if (!search_string_index)
  4690.         continue;
  4691.           else
  4692.         {
  4693.           if (reverse)
  4694.             --index;
  4695.           else
  4696.             if (index != strlen (sline))
  4697.               ++index;
  4698.             else
  4699.               ding ();
  4700.         }
  4701.         do_search:
  4702.  
  4703.           while (1)
  4704.         {
  4705.           if (reverse)
  4706.             {
  4707.               while (index >= 0)
  4708.             if (strncmp
  4709.                 (search_string, sline + index, search_string_index)
  4710.                 == 0)
  4711.               goto string_found;
  4712.             else
  4713.               index--;
  4714.             }
  4715.           else
  4716.             {
  4717.               register int limit =
  4718.             (strlen (sline) - search_string_index) + 1;
  4719.  
  4720.               while (index < limit)
  4721.             {
  4722.               if (strncmp (search_string,
  4723.                        sline + index,
  4724.                        search_string_index) == 0)
  4725.                 goto string_found;
  4726.               index++;
  4727.             }
  4728.             }
  4729.  
  4730.         next_line:
  4731.           i += direction;
  4732.  
  4733.           /* At limit for direction? */
  4734.           if ((reverse && i < 0) ||
  4735.               (!reverse && i == hlen))
  4736.             goto search_failed;
  4737.  
  4738.           sline = lines[i];
  4739.           if (reverse)
  4740.             index = strlen (sline);
  4741.           else
  4742.             index = 0;
  4743.  
  4744.           /* If the search string is longer than the current
  4745.              line, no match. */
  4746.           if (search_string_index > strlen (sline))
  4747.             goto next_line;
  4748.  
  4749.           /* Start actually searching. */
  4750.           if (reverse)
  4751.             index -= search_string_index;
  4752.         }
  4753.  
  4754.         search_failed:
  4755.           /* We cannot find the search string.  Ding the bell. */
  4756.           ding ();
  4757.           i = last_found_line;
  4758.           break;
  4759.  
  4760.         string_found:
  4761.           /* We have found the search string.  Just display it.  But don't
  4762.          actually move there in the history list until the user accepts
  4763.          the location. */
  4764.           {
  4765.         int line_len;
  4766.  
  4767.         line_len = strlen (lines[i]);
  4768.  
  4769.         if (line_len >= rl_line_buffer_len)
  4770.           rl_extend_line_buffer (line_len);
  4771.  
  4772.         strcpy (the_line, lines[i]);
  4773.         rl_point = index;
  4774.         rl_end = line_len;
  4775.         last_found_line = i;
  4776.         rl_display_search
  4777.           (search_string, reverse, (i == orig_line) ? -1 : i);
  4778.           }
  4779.         }
  4780.     }
  4781.       continue;
  4782.     }
  4783.  
  4784.   /* The searching is over.  The user may have found the string that she
  4785.      was looking for, or else she may have exited a failing search.  If
  4786.      INDEX is -1, then that shows that the string searched for was not
  4787.      found.  We use this to determine where to place rl_point. */
  4788.   {
  4789.     int now = last_found_line;
  4790.  
  4791.     /* First put back the original state. */
  4792.     strcpy (the_line, lines[orig_line]);
  4793.  
  4794.     if (now < orig_line)
  4795.       rl_get_previous_history (orig_line - now);
  4796.     else
  4797.       rl_get_next_history (now - orig_line);
  4798.  
  4799.     /* If the index of the "matched" string is less than zero, then the
  4800.        final search string was never matched, so put point somewhere
  4801.        reasonable. */
  4802.     if (index < 0)
  4803.       index = strlen (the_line);
  4804.  
  4805.     rl_point = index;
  4806.     rl_clear_message ();
  4807.   }
  4808. }
  4809.  
  4810. /* Make C be the next command to be executed. */
  4811. rl_execute_next (c)
  4812.      int c;
  4813. {
  4814.   rl_pending_input = c;
  4815. }
  4816.  
  4817. /* **************************************************************** */
  4818. /*                                    */
  4819. /*            Killing Mechanism                */
  4820. /*                                    */
  4821. /* **************************************************************** */
  4822.  
  4823. /* What we assume for a max number of kills. */
  4824. #define DEFAULT_MAX_KILLS 10
  4825.  
  4826. /* The real variable to look at to find out when to flush kills. */
  4827. int rl_max_kills = DEFAULT_MAX_KILLS;
  4828.  
  4829. /* Where to store killed text. */
  4830. char **rl_kill_ring = (char **)NULL;
  4831.  
  4832. /* Where we are in the kill ring. */
  4833. int rl_kill_index = 0;
  4834.  
  4835. /* How many slots we have in the kill ring. */
  4836. int rl_kill_ring_length = 0;
  4837.  
  4838. /* How to say that you only want to save a certain amount
  4839.    of kill material. */
  4840. rl_set_retained_kills (num)
  4841.      int num;
  4842. {}
  4843.  
  4844. /* The way to kill something.  This appends or prepends to the last
  4845.    kill, if the last command was a kill command.  if FROM is less
  4846.    than TO, then the text is appended, otherwise prepended.  If the
  4847.    last command was not a kill command, then a new slot is made for
  4848.    this kill. */
  4849. rl_kill_text (from, to)
  4850.      int from, to;
  4851. {
  4852.   int slot;
  4853.   char *text = rl_copy (from, to);
  4854.  
  4855.   /* Is there anything to kill? */
  4856.   if (from == to)
  4857.     {
  4858.       free (text);
  4859.       last_command_was_kill++;
  4860.       return;
  4861.     }
  4862.  
  4863.   /* Delete the copied text from the line. */
  4864.   rl_delete_text (from, to);
  4865.  
  4866.   /* First, find the slot to work with. */
  4867.   if (!last_command_was_kill)
  4868.     {
  4869.       /* Get a new slot.  */
  4870.       if (!rl_kill_ring)
  4871.     {
  4872.       /* If we don't have any defined, then make one. */
  4873.       rl_kill_ring = (char **)
  4874.         xmalloc (((rl_kill_ring_length = 1) + 1) * sizeof (char *));
  4875.       slot = 1;
  4876.     }
  4877.       else
  4878.     {
  4879.       /* We have to add a new slot on the end, unless we have
  4880.          exceeded the max limit for remembering kills. */
  4881.       slot = rl_kill_ring_length;
  4882.       if (slot == rl_max_kills)
  4883.         {
  4884.           register int i;
  4885.           free (rl_kill_ring[0]);
  4886.           for (i = 0; i < slot; i++)
  4887.         rl_kill_ring[i] = rl_kill_ring[i + 1];
  4888.         }
  4889.       else
  4890.         {
  4891.           rl_kill_ring =
  4892.         (char **)
  4893.           xrealloc (rl_kill_ring,
  4894.                 ((slot = (rl_kill_ring_length += 1)) + 1)
  4895.                 * sizeof (char *));
  4896.         }
  4897.     }
  4898.       slot--;
  4899.     }
  4900.   else
  4901.     {
  4902.       slot = rl_kill_ring_length - 1;
  4903.     }
  4904.  
  4905.   /* If the last command was a kill, prepend or append. */
  4906.   if (last_command_was_kill && rl_editing_mode != vi_mode)
  4907.     {
  4908.       char *old = rl_kill_ring[slot];
  4909.       char *new = (char *)xmalloc (1 + strlen (old) + strlen (text));
  4910.  
  4911.       if (from < to)
  4912.     {
  4913.       strcpy (new, old);
  4914.       strcat (new, text);
  4915.     }
  4916.       else
  4917.     {
  4918.       strcpy (new, text);
  4919.       strcat (new, old);
  4920.     }
  4921.       free (old);
  4922.       free (text);
  4923.       rl_kill_ring[slot] = new;
  4924.     }
  4925.   else
  4926.     {
  4927.       rl_kill_ring[slot] = text;
  4928.     }
  4929.   rl_kill_index = slot;
  4930.   last_command_was_kill++;
  4931. }
  4932.  
  4933. /* Now REMEMBER!  In order to do prepending or appending correctly, kill
  4934.    commands always make rl_point's original position be the FROM argument,
  4935.    and rl_point's extent be the TO argument. */
  4936.  
  4937. /* **************************************************************** */
  4938. /*                                    */
  4939. /*            Killing Commands                */
  4940. /*                                    */
  4941. /* **************************************************************** */
  4942.  
  4943. /* Delete the word at point, saving the text in the kill ring. */
  4944. rl_kill_word (count)
  4945.      int count;
  4946. {
  4947.   int orig_point = rl_point;
  4948.  
  4949.   if (count < 0)
  4950.     rl_backward_kill_word (-count);
  4951.   else
  4952.     {
  4953.       rl_forward_word (count);
  4954.  
  4955.       if (rl_point != orig_point)
  4956.     rl_kill_text (orig_point, rl_point);
  4957.  
  4958.       rl_point = orig_point;
  4959.     }
  4960. }
  4961.  
  4962. /* Rubout the word before point, placing it on the kill ring. */
  4963. rl_backward_kill_word (count)
  4964.      int count;
  4965. {
  4966.   int orig_point = rl_point;
  4967.  
  4968.   if (count < 0)
  4969.     rl_kill_word (-count);
  4970.   else
  4971.     {
  4972.       rl_backward_word (count);
  4973.  
  4974.       if (rl_point != orig_point)
  4975.     rl_kill_text (orig_point, rl_point);
  4976.     }
  4977. }
  4978.  
  4979. /* Kill from here to the end of the line.  If DIRECTION is negative, kill
  4980.    back to the line start instead. */
  4981. rl_kill_line (direction)
  4982.      int direction;
  4983. {
  4984.   int orig_point = rl_point;
  4985.  
  4986.   if (direction < 0)
  4987.     rl_backward_kill_line (1);
  4988.   else
  4989.     {
  4990.       rl_end_of_line ();
  4991.       if (orig_point != rl_point)
  4992.     rl_kill_text (orig_point, rl_point);
  4993.       rl_point = orig_point;
  4994.     }
  4995. }
  4996.  
  4997. /* Kill backwards to the start of the line.  If DIRECTION is negative, kill
  4998.    forwards to the line end instead. */
  4999. rl_backward_kill_line (direction)
  5000.      int direction;
  5001. {
  5002.   int orig_point = rl_point;
  5003.  
  5004.   if (direction < 0)
  5005.     rl_kill_line (1);
  5006.   else
  5007.     {
  5008.       if (!rl_point)
  5009.     ding ();
  5010.       else
  5011.     {
  5012.       rl_beg_of_line ();
  5013.       rl_kill_text (orig_point, rl_point);
  5014.     }
  5015.     }
  5016. }
  5017.  
  5018. /* Yank back the last killed text.  This ignores arguments. */
  5019. rl_yank ()
  5020. {
  5021.   if (!rl_kill_ring) rl_abort ();
  5022.   rl_insert_text (rl_kill_ring[rl_kill_index]);
  5023. }
  5024.  
  5025. /* If the last command was yank, or yank_pop, and the text just
  5026.    before point is identical to the current kill item, then
  5027.    delete that text from the line, rotate the index down, and
  5028.    yank back some other text. */
  5029. rl_yank_pop ()
  5030. {
  5031.   int l;
  5032.  
  5033.   if (((rl_last_func != rl_yank_pop) && (rl_last_func != rl_yank)) ||
  5034.       !rl_kill_ring)
  5035.     {
  5036.       rl_abort ();
  5037.     }
  5038.  
  5039.   l = strlen (rl_kill_ring[rl_kill_index]);
  5040.   if (((rl_point - l) >= 0) &&
  5041.       (strncmp (the_line + (rl_point - l),
  5042.         rl_kill_ring[rl_kill_index], l) == 0))
  5043.     {
  5044.       rl_delete_text ((rl_point - l), rl_point);
  5045.       rl_point -= l;
  5046.       rl_kill_index--;
  5047.       if (rl_kill_index < 0)
  5048.     rl_kill_index = rl_kill_ring_length - 1;
  5049.       rl_yank ();
  5050.     }
  5051.   else
  5052.     rl_abort ();
  5053.  
  5054. }
  5055.  
  5056. /* Yank the COUNTth argument from the previous history line. */
  5057. rl_yank_nth_arg (count, ignore)
  5058.      int count;
  5059. {
  5060.   register HIST_ENTRY *entry = previous_history ();
  5061.   char *arg;
  5062.  
  5063.   if (entry)
  5064.     next_history ();
  5065.   else
  5066.     {
  5067.       ding ();
  5068.       return;
  5069.     }
  5070.  
  5071.   arg = history_arg_extract (count, count, entry->line);
  5072.   if (!arg || !*arg)
  5073.     {
  5074.       ding ();
  5075.       return;
  5076.     }
  5077.  
  5078.   rl_begin_undo_group ();
  5079.  
  5080. #if defined (VI_MODE)
  5081.   /* Vi mode always inserts a space befoe yanking the argument, and it
  5082.      inserts it right *after* rl_point. */
  5083.   if (rl_editing_mode == vi_mode)
  5084.     rl_point++;
  5085. #endif /* VI_MODE */
  5086.  
  5087.   if (rl_point && the_line[rl_point - 1] != ' ')
  5088.     rl_insert_text (" ");
  5089.  
  5090.   rl_insert_text (arg);
  5091.   free (arg);
  5092.  
  5093.   rl_end_undo_group ();
  5094. }
  5095.  
  5096. /* How to toggle back and forth between editing modes. */
  5097. rl_vi_editing_mode ()
  5098. {
  5099. #if defined (VI_MODE)
  5100.   rl_editing_mode = vi_mode;
  5101.   rl_vi_insertion_mode ();
  5102. #endif /* VI_MODE */
  5103. }
  5104.  
  5105. rl_emacs_editing_mode ()
  5106. {
  5107.   rl_editing_mode = emacs_mode;
  5108.   keymap = emacs_standard_keymap;
  5109. }
  5110.  
  5111.  
  5112. /* **************************************************************** */
  5113. /*                                    */
  5114. /*                 Completion                    */
  5115. /*                                    */
  5116. /* **************************************************************** */
  5117.  
  5118. /* Non-zero means that case is not significant in completion. */
  5119. int completion_case_fold = 0;
  5120.  
  5121. /* Return an array of (char *) which is a list of completions for TEXT.
  5122.    If there are no completions, return a NULL pointer.
  5123.    The first entry in the returned array is the substitution for TEXT.
  5124.    The remaining entries are the possible completions.
  5125.    The array is terminated with a NULL pointer.
  5126.  
  5127.    ENTRY_FUNCTION is a function of two args, and returns a (char *).
  5128.      The first argument is TEXT.
  5129.      The second is a state argument; it should be zero on the first call, and
  5130.      non-zero on subsequent calls.  It returns a NULL pointer to the caller
  5131.      when there are no more matches.
  5132.  */
  5133. char **
  5134. completion_matches (text, entry_function)
  5135.      char *text;
  5136.      char *(*entry_function) ();
  5137. {
  5138.   /* Number of slots in match_list. */
  5139.   int match_list_size;
  5140.  
  5141.   /* The list of matches. */
  5142.   char **match_list =
  5143.     (char **)xmalloc (((match_list_size = 10) + 1) * sizeof (char *));
  5144.  
  5145.   /* Number of matches actually found. */
  5146.   int matches = 0;
  5147.  
  5148.   /* Temporary string binder. */
  5149.   char *string;
  5150.  
  5151.   match_list[1] = (char *)NULL;
  5152.  
  5153.   while (string = (*entry_function) (text, matches))
  5154.     {
  5155.       if (matches + 1 == match_list_size)
  5156.     match_list = (char **)xrealloc
  5157.       (match_list, ((match_list_size += 10) + 1) * sizeof (char *));
  5158.  
  5159.       match_list[++matches] = string;
  5160.       match_list[matches + 1] = (char *)NULL;
  5161.     }
  5162.  
  5163.   /* If there were any matches, then look through them finding out the
  5164.      lowest common denominator.  That then becomes match_list[0]. */
  5165.   if (matches)
  5166.     {
  5167.       register int i = 1;
  5168.       int low = 100000;        /* Count of max-matched characters. */
  5169.  
  5170.       /* If only one match, just use that. */
  5171.       if (matches == 1)
  5172.     {
  5173.       match_list[0] = match_list[1];
  5174.       match_list[1] = (char *)NULL;
  5175.     }
  5176.       else
  5177.     {
  5178.       /* Otherwise, compare each member of the list with
  5179.          the next, finding out where they stop matching. */
  5180.  
  5181.       while (i < matches)
  5182.         {
  5183.           register int c1, c2, si;
  5184.  
  5185.           if (completion_case_fold)
  5186.         {
  5187.           for (si = 0;
  5188.                (c1 = to_lower(match_list[i][si])) &&
  5189.                (c2 = to_lower(match_list[i + 1][si]));
  5190.                si++)
  5191.             if (c1 != c2) break;
  5192.         }
  5193.           else
  5194.         {
  5195.           for (si = 0;
  5196.                (c1 = match_list[i][si]) &&
  5197.                (c2 = match_list[i + 1][si]);
  5198.                si++)
  5199.             if (c1 != c2) break;
  5200.         }
  5201.  
  5202.           if (low > si) low = si;
  5203.           i++;
  5204.         }
  5205.       match_list[0] = (char *)xmalloc (low + 1);
  5206.       strncpy (match_list[0], match_list[1], low);
  5207.       match_list[0][low] = '\0';
  5208.     }
  5209.     }
  5210.   else                /* There were no matches. */
  5211.     {
  5212.       free (match_list);
  5213.       match_list = (char **)NULL;
  5214.     }
  5215.   return (match_list);
  5216. }
  5217.  
  5218. /* Okay, now we write the entry_function for filename completion.  In the
  5219.    general case.  Note that completion in the shell is a little different
  5220.    because of all the pathnames that must be followed when looking up the
  5221.    completion for a command. */
  5222. char *
  5223. filename_completion_function (text, state)
  5224.      int state;
  5225.      char *text;
  5226. {
  5227.   static DIR *directory;
  5228.   static char *filename = (char *)NULL;
  5229.   static char *dirname = (char *)NULL;
  5230.   static char *users_dirname = (char *)NULL;
  5231.   static int filename_len;
  5232.  
  5233.   dirent *entry = (dirent *)NULL;
  5234.  
  5235.   /* If we don't have any state, then do some initialization. */
  5236.   if (!state)
  5237.     {
  5238.       char *temp;
  5239.  
  5240.       if (dirname) free (dirname);
  5241.       if (filename) free (filename);
  5242.       if (users_dirname) free (users_dirname);
  5243.  
  5244.       filename = savestring (text);
  5245.       if (!*text) text = ".";
  5246.       dirname = savestring (text);
  5247.  
  5248.       temp = rindex (dirname, '/');
  5249.  
  5250.       if (temp)
  5251.     {
  5252.       strcpy (filename, ++temp);
  5253.       *temp = '\0';
  5254.     }
  5255.       else
  5256.     strcpy (dirname, ".");
  5257.  
  5258.       /* We aren't done yet.  We also support the "~user" syntax. */
  5259.  
  5260.       /* Save the version of the directory that the user typed. */
  5261.       users_dirname = savestring (dirname);
  5262.       {
  5263.     char *temp_dirname;
  5264.  
  5265.     temp_dirname = tilde_expand (dirname);
  5266.     free (dirname);
  5267.     dirname = temp_dirname;
  5268.  
  5269.     if (rl_symbolic_link_hook)
  5270.       (*rl_symbolic_link_hook) (&dirname);
  5271.       }
  5272.       directory = opendir (dirname);
  5273.       filename_len = strlen (filename);
  5274.  
  5275.       rl_filename_completion_desired = 1;
  5276.     }
  5277.  
  5278.   /* At this point we should entertain the possibility of hacking wildcarded
  5279.      filenames, like /usr/man/man<WILD>/te<TAB>.  If the directory name
  5280.      contains globbing characters, then build an array of directories to
  5281.      glob on, and glob on the first one. */
  5282.  
  5283.   /* Now that we have some state, we can read the directory. */
  5284.  
  5285.   while (directory && (entry = readdir (directory)))
  5286.     {
  5287.       /* Special case for no filename.
  5288.      All entries except "." and ".." match. */
  5289.       if (!filename_len)
  5290.     {
  5291.       if ((strcmp (entry->d_name, ".") != 0) &&
  5292.           (strcmp (entry->d_name, "..") != 0))
  5293.         break;
  5294.     }
  5295.       else
  5296.     {
  5297.       /* Otherwise, if these match upto the length of filename, then
  5298.          it is a match. */
  5299.         if (entry->d_name[0] == filename[0] && /* Quick test */
  5300.         (strncmp (filename, entry->d_name, filename_len) == 0))
  5301.           {
  5302.         break;
  5303.           }
  5304.     }
  5305.     }
  5306.  
  5307.   if (!entry)
  5308.     {
  5309.       if (directory)
  5310.     {
  5311.       closedir (directory);
  5312.       directory = (DIR *)NULL;
  5313.     }
  5314.       return (char *)NULL;
  5315.     }
  5316.   else
  5317.     {
  5318.       char *temp;
  5319.  
  5320.       if (dirname && (strcmp (dirname, ".") != 0))
  5321.     {
  5322.       temp = (char *)
  5323.         xmalloc (1 + strlen (users_dirname) + strlen (entry->d_name));
  5324.       strcpy (temp, users_dirname);
  5325.       strcat (temp, entry->d_name);
  5326.     }
  5327.       else
  5328.     {
  5329.       temp = (savestring (entry->d_name));
  5330.     }
  5331.       return (temp);
  5332.     }
  5333. }
  5334.  
  5335.  
  5336. /* **************************************************************** */
  5337. /*                                    */
  5338. /*            Binding keys                    */
  5339. /*                                    */
  5340. /* **************************************************************** */
  5341.  
  5342. /* rl_add_defun (char *name, Function *function, int key)
  5343.    Add NAME to the list of named functions.  Make FUNCTION
  5344.    be the function that gets called.
  5345.    If KEY is not -1, then bind it. */
  5346. rl_add_defun (name, function, key)
  5347.      char *name;
  5348.      Function *function;
  5349.      int key;
  5350. {
  5351.   if (key != -1)
  5352.     rl_bind_key (key, function);
  5353.   rl_add_funmap_entry (name, function);
  5354. }
  5355.  
  5356. /* Bind KEY to FUNCTION.  Returns non-zero if KEY is out of range. */
  5357. int
  5358. rl_bind_key (key, function)
  5359.      int key;
  5360.      Function *function;
  5361. {
  5362.   if (key < 0)
  5363.     return (key);
  5364.  
  5365.   if (key > 127 && key < 256)
  5366.     {
  5367.       if (keymap[ESC].type == ISKMAP)
  5368.     {
  5369.       Keymap escmap = (Keymap)keymap[ESC].function;
  5370.  
  5371.       key -= 128;
  5372.       escmap[key].type = ISFUNC;
  5373.       escmap[key].function = function;
  5374.       return (0);
  5375.     }
  5376.       return (key);
  5377.     }
  5378.  
  5379.   keymap[key].type = ISFUNC;
  5380.   keymap[key].function = function;
  5381.  return (0);
  5382. }
  5383.  
  5384. /* Bind KEY to FUNCTION in MAP.  Returns non-zero in case of invalid
  5385.    KEY. */
  5386. int
  5387. rl_bind_key_in_map (key, function, map)
  5388.      int key;
  5389.      Function *function;
  5390.      Keymap map;
  5391. {
  5392.   int result;
  5393.   Keymap oldmap = keymap;
  5394.  
  5395.   keymap = map;
  5396.   result = rl_bind_key (key, function);
  5397.   keymap = oldmap;
  5398.   return (result);
  5399. }
  5400.  
  5401. /* Make KEY do nothing in the currently selected keymap.
  5402.    Returns non-zero in case of error. */
  5403. int
  5404. rl_unbind_key (key)
  5405.      int key;
  5406. {
  5407.   return (rl_bind_key (key, (Function *)NULL));
  5408. }
  5409.  
  5410. /* Make KEY do nothing in MAP.
  5411.    Returns non-zero in case of error. */
  5412. int
  5413. rl_unbind_key_in_map (key, map)
  5414.      int key;
  5415.      Keymap map;
  5416. {
  5417.   return (rl_bind_key_in_map (key, (Function *)NULL, map));
  5418. }
  5419.  
  5420. /* Bind the key sequence represented by the string KEYSEQ to
  5421.    FUNCTION.  This makes new keymaps as necessary.  The initial
  5422.    place to do bindings is in MAP. */
  5423. rl_set_key (keyseq, function, map)
  5424.      char *keyseq;
  5425.      Function *function;
  5426.      Keymap map;
  5427. {
  5428.   rl_generic_bind (ISFUNC, keyseq, function, map);
  5429. }
  5430.  
  5431. /* Bind the key sequence represented by the string KEYSEQ to
  5432.    the string of characters MACRO.  This makes new keymaps as
  5433.    necessary.  The initial place to do bindings is in MAP. */
  5434. rl_macro_bind (keyseq, macro, map)
  5435.      char *keyseq, *macro;
  5436.      Keymap map;
  5437. {
  5438.   char *macro_keys;
  5439.   int macro_keys_len;
  5440.  
  5441.   macro_keys = (char *)xmalloc ((2 * strlen (macro)) + 1);
  5442.  
  5443.   if (rl_translate_keyseq (macro, macro_keys, ¯o_keys_len))
  5444.     {
  5445.       free (macro_keys);
  5446.       return;
  5447.     }
  5448.   rl_generic_bind (ISMACR, keyseq, macro_keys, map);
  5449. }
  5450.  
  5451. /* Bind the key sequence represented by the string KEYSEQ to
  5452.    the arbitrary pointer DATA.  TYPE says what kind of data is
  5453.    pointed to by DATA, right now this can be a function (ISFUNC),
  5454.    a macro (ISMACR), or a keymap (ISKMAP).  This makes new keymaps
  5455.    as necessary.  The initial place to do bindings is in MAP. */
  5456. rl_generic_bind (type, keyseq, data, map)
  5457.      int type;
  5458.      char *keyseq, *data;
  5459.      Keymap map;
  5460. {
  5461.   char *keys;
  5462.   int keys_len;
  5463.   register int i;
  5464.  
  5465.   /* If no keys to bind to, exit right away. */
  5466.   if (!keyseq || !*keyseq)
  5467.     {
  5468.       if (type == ISMACR)
  5469.     free (data);
  5470.       return;
  5471.     }
  5472.  
  5473.   keys = (char *)alloca (1 + (2 * strlen (keyseq)));
  5474.  
  5475.   /* Translate the ASCII representation of KEYSEQ into an array
  5476.      of characters.  Stuff the characters into ARRAY, and the
  5477.      length of ARRAY into LENGTH. */
  5478.   if (rl_translate_keyseq (keyseq, keys, &keys_len))
  5479.     return;
  5480.  
  5481.   /* Bind keys, making new keymaps as necessary. */
  5482.   for (i = 0; i < keys_len; i++)
  5483.     {
  5484.       if (i + 1 < keys_len)
  5485.     {
  5486.       if (map[keys[i]].type != ISKMAP)
  5487.         {
  5488.           if (map[i].type == ISMACR)
  5489.         free ((char *)map[i].function);
  5490.  
  5491.           map[keys[i]].type = ISKMAP;
  5492.           map[keys[i]].function = (Function *)rl_make_bare_keymap ();
  5493.         }
  5494.       map = (Keymap)map[keys[i]].function;
  5495.     }
  5496.       else
  5497.     {
  5498.       if (map[keys[i]].type == ISMACR)
  5499.         free ((char *)map[keys[i]].function);
  5500.  
  5501.       map[keys[i]].function = (Function *)data;
  5502.       map[keys[i]].type = type;
  5503.     }
  5504.     }
  5505. }
  5506.  
  5507. /* Translate the ASCII representation of SEQ, stuffing the
  5508.    values into ARRAY, an array of characters.  LEN gets the
  5509.    final length of ARRAY.  Return non-zero if there was an
  5510.    error parsing SEQ. */
  5511. rl_translate_keyseq (seq, array, len)
  5512.      char *seq, *array;
  5513.      int *len;
  5514. {
  5515.   register int i, c, l = 0;
  5516.  
  5517.   for (i = 0; c = seq[i]; i++)
  5518.     {
  5519.       if (c == '\\')
  5520.     {
  5521.       c = seq[++i];
  5522.  
  5523.       if (!c)
  5524.         break;
  5525.  
  5526.       if (((c == 'C' || c == 'M') &&  seq[i + 1] == '-') ||
  5527.           (c == 'e'))
  5528.         {
  5529.           /* Handle special case of backwards define. */
  5530.           if (strncmp (&seq[i], "C-\\M-", 5) == 0)
  5531.         {
  5532.           array[l++] = ESC;
  5533.           i += 5;
  5534.           array[l++] = CTRL (to_upper (seq[i]));
  5535.           if (!seq[i])
  5536.             i--;
  5537.           continue;
  5538.         }
  5539.  
  5540.           switch (c)
  5541.         {
  5542.         case 'M':
  5543.           i++;
  5544.           array[l++] = ESC;
  5545.           break;
  5546.  
  5547.         case 'C':
  5548.           i += 2;
  5549.           /* Special hack for C-?... */
  5550.           if (seq[i] == '?')
  5551.             array[l++] = RUBOUT;
  5552.           else
  5553.             array[l++] = CTRL (to_upper (seq[i]));
  5554.           break;
  5555.  
  5556.         case 'e':
  5557.           array[l++] = ESC;
  5558.         }
  5559.  
  5560.           continue;
  5561.         }
  5562.     }
  5563.       array[l++] = c;
  5564.     }
  5565.  
  5566.   *len = l;
  5567.   array[l] = '\0';
  5568.   return (0);
  5569. }
  5570.  
  5571. /* Return a pointer to the function that STRING represents.
  5572.    If STRING doesn't have a matching function, then a NULL pointer
  5573.    is returned. */
  5574. Function *
  5575. rl_named_function (string)
  5576.      char *string;
  5577. {
  5578.   register int i;
  5579.  
  5580.   for (i = 0; funmap[i]; i++)
  5581.     if (stricmp (funmap[i]->name, string) == 0)
  5582.       return (funmap[i]->function);
  5583.   return ((Function *)NULL);
  5584. }
  5585.  
  5586. /* The last key bindings file read. */
  5587. #ifdef __MSDOS__
  5588. /* Don't know what to do, but this is a guess */
  5589. static char *last_readline_init_file = "/INPUTRC";
  5590. #else
  5591. static char *last_readline_init_file = "~/inputrc";
  5592. #endif
  5593.  
  5594. /* Re-read the current keybindings file. */
  5595. rl_re_read_init_file (count, ignore)
  5596.      int count, ignore;
  5597. {
  5598.   rl_read_init_file ((char *)NULL);
  5599. }
  5600.  
  5601. /* Do key bindings from a file.  If FILENAME is NULL it defaults
  5602.    to `~/.inputrc'.  If the file existed and could be opened and
  5603.    read, 0 is returned, otherwise errno is returned. */
  5604. int
  5605. rl_read_init_file (filename)
  5606.      char *filename;
  5607. {
  5608.   register int i;
  5609.   char *buffer, *openname, *line, *end;
  5610.   struct stat finfo;
  5611.   int file;
  5612.  
  5613.   /* Default the filename. */
  5614.   if (!filename)
  5615.     filename = last_readline_init_file;
  5616.  
  5617.   openname = tilde_expand (filename);
  5618.  
  5619.   if (!openname || *openname == '\000')
  5620.     return ENOENT;
  5621.  
  5622.   if ((stat (openname, &finfo) < 0) ||
  5623.       (file = open (openname, O_RDONLY, 0666)) < 0)
  5624.     {
  5625.       free (openname);
  5626.       return (errno);
  5627.     }
  5628.   else
  5629.     free (openname);
  5630.  
  5631.   last_readline_init_file = filename;
  5632.  
  5633.   /* Read the file into BUFFER. */
  5634.   buffer = (char *)xmalloc (finfo.st_size + 1);
  5635.   i = read (file, buffer, finfo.st_size);
  5636.   close (file);
  5637.  
  5638.   if (i != finfo.st_size)
  5639.     return (errno);
  5640.  
  5641.   /* Loop over the lines in the file.  Lines that start with `#' are
  5642.      comments; all other lines are commands for readline initialization. */
  5643.   line = buffer;
  5644.   end = buffer + finfo.st_size;
  5645.   while (line < end)
  5646.     {
  5647.       /* Find the end of this line. */
  5648.       for (i = 0; line + i != end && line[i] != '\n'; i++);
  5649.  
  5650.       /* Mark end of line. */
  5651.       line[i] = '\0';
  5652.  
  5653.       /* If the line is not a comment, then parse it. */
  5654.       if (*line != '#')
  5655.     rl_parse_and_bind (line);
  5656.  
  5657.       /* Move to the next line. */
  5658.       line += i + 1;
  5659.     }
  5660.   return (0);
  5661. }
  5662.  
  5663. /* **************************************************************** */
  5664. /*                                    */
  5665. /*            Parser Directives                   */
  5666. /*                                    */
  5667. /* **************************************************************** */
  5668.  
  5669. /* Conditionals. */
  5670.  
  5671. /* Calling programs set this to have their argv[0]. */
  5672. char *rl_readline_name = "other";
  5673.  
  5674. /* Stack of previous values of parsing_conditionalized_out. */
  5675. static unsigned char *if_stack = (unsigned char *)NULL;
  5676. static int if_stack_depth = 0;
  5677. static int if_stack_size = 0;
  5678.  
  5679. /* Push parsing_conditionalized_out, and set parser state based on ARGS. */
  5680. parser_if (args)
  5681.      char *args;
  5682. {
  5683.   register int i;
  5684.  
  5685.   /* Push parser state. */
  5686.   if (if_stack_depth + 1 >= if_stack_size)
  5687.     {
  5688.       if (!if_stack)
  5689.     if_stack = (unsigned char *)xmalloc (if_stack_size = 20);
  5690.       else
  5691.     if_stack = (unsigned char *)xrealloc (if_stack, if_stack_size += 20);
  5692.     }
  5693.   if_stack[if_stack_depth++] = parsing_conditionalized_out;
  5694.  
  5695.   /* If parsing is turned off, then nothing can turn it back on except
  5696.      for finding the matching endif.  In that case, return right now. */
  5697.   if (parsing_conditionalized_out)
  5698.     return;
  5699.  
  5700.   /* Isolate first argument. */
  5701.   for (i = 0; args[i] && !whitespace (args[i]); i++);
  5702.  
  5703.   if (args[i])
  5704.     args[i++] = '\0';
  5705.  
  5706.   /* Handle "if term=foo" and "if mode=emacs" constructs.  If this
  5707.      isn't term=foo, or mode=emacs, then check to see if the first
  5708.      word in ARGS is the same as the value stored in rl_readline_name. */
  5709.   if (rl_terminal_name && strnicmp (args, "term=", 5) == 0)
  5710.     {
  5711.       char *tem, *tname;
  5712.  
  5713.       /* Terminals like "aaa-60" are equivalent to "aaa". */
  5714.       tname = savestring (rl_terminal_name);
  5715.       tem = rindex (tname, '-');
  5716.       if (tem)
  5717.     *tem = '\0';
  5718.  
  5719.       if (stricmp (args + 5, tname) == 0)
  5720.     parsing_conditionalized_out = 0;
  5721.       else
  5722.     parsing_conditionalized_out = 1;
  5723.     }
  5724. #if defined (VI_MODE)
  5725.   else if (strnicmp (args, "mode=", 5) == 0)
  5726.     {
  5727.       int mode;
  5728.  
  5729.       if (stricmp (args + 5, "emacs") == 0)
  5730.     mode = emacs_mode;
  5731.       else if (stricmp (args + 5, "vi") == 0)
  5732.     mode = vi_mode;
  5733.       else
  5734.     mode = no_mode;
  5735.  
  5736.       if (mode == rl_editing_mode)
  5737.     parsing_conditionalized_out = 0;
  5738.       else
  5739.     parsing_conditionalized_out = 1;
  5740.     }
  5741. #endif /* VI_MODE */
  5742.   /* Check to see if the first word in ARGS is the same as the
  5743.      value stored in rl_readline_name. */
  5744.   else if (stricmp (args, rl_readline_name) == 0)
  5745.     parsing_conditionalized_out = 0;
  5746.   else
  5747.     parsing_conditionalized_out = 1;
  5748. }
  5749.  
  5750. /* Invert the current parser state if there is anything on the stack. */
  5751. parser_else (args)
  5752.      char *args;
  5753. {
  5754.   register int i;
  5755.  
  5756.   if (!if_stack_depth)
  5757.     {
  5758.       /* Error message? */
  5759.       return;
  5760.     }
  5761.  
  5762.   /* Check the previous (n - 1) levels of the stack to make sure that
  5763.      we haven't previously turned off parsing. */
  5764.   for (i = 0; i < if_stack_depth - 1; i++)
  5765.     if (if_stack[i] == 1)
  5766.       return;
  5767.  
  5768.   /* Invert the state of parsing if at top level. */
  5769.   parsing_conditionalized_out = !parsing_conditionalized_out;
  5770. }
  5771.  
  5772. /* Terminate a conditional, popping the value of
  5773.    parsing_conditionalized_out from the stack. */
  5774. parser_endif (args)
  5775.      char *args;
  5776. {
  5777.   if (if_stack_depth)
  5778.     parsing_conditionalized_out = if_stack[--if_stack_depth];
  5779.   else
  5780.     {
  5781.       /* *** What, no error message? *** */
  5782.     }
  5783. }
  5784.  
  5785. /* Associate textual names with actual functions. */
  5786. static struct {
  5787.   char *name;
  5788.   Function *function;
  5789. } parser_directives [] = {
  5790.   { "if", parser_if },
  5791.   { "endif", parser_endif },
  5792.   { "else", parser_else },
  5793.   { (char *)0x0, (Function *)0x0 }
  5794. };
  5795.  
  5796. /* Handle a parser directive.  STATEMENT is the line of the directive
  5797.    without any leading `$'. */
  5798. static int
  5799. handle_parser_directive (statement)
  5800.      char *statement;
  5801. {
  5802.   register int i;
  5803.   char *directive, *args;
  5804.  
  5805.   /* Isolate the actual directive. */
  5806.  
  5807.   /* Skip whitespace. */
  5808.   for (i = 0; whitespace (statement[i]); i++);
  5809.  
  5810.   directive = &statement[i];
  5811.  
  5812.   for (; statement[i] && !whitespace (statement[i]); i++);
  5813.  
  5814.   if (statement[i])
  5815.     statement[i++] = '\0';
  5816.  
  5817.   for (; statement[i] && whitespace (statement[i]); i++);
  5818.  
  5819.   args = &statement[i];
  5820.  
  5821.   /* Lookup the command, and act on it. */
  5822.   for (i = 0; parser_directives[i].name; i++)
  5823.     if (stricmp (directive, parser_directives[i].name) == 0)
  5824.       {
  5825.     (*parser_directives[i].function) (args);
  5826.     return (0);
  5827.       }
  5828.  
  5829.   /* *** Should an error message be output? */
  5830.   return (1);
  5831. }
  5832.  
  5833. /* Ugly but working hack for binding prefix meta. */
  5834. #define PREFIX_META_HACK
  5835.  
  5836. static int substring_member_of_array ();
  5837.  
  5838. /* Read the binding command from STRING and perform it.
  5839.    A key binding command looks like: Keyname: function-name\0,
  5840.    a variable binding command looks like: set variable value.
  5841.    A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */
  5842. rl_parse_and_bind (string)
  5843.      char *string;
  5844. {
  5845.   extern char *possible_control_prefixes[], *possible_meta_prefixes[];
  5846.   char *funname, *kname;
  5847.   register int c;
  5848.   int key, i;
  5849.  
  5850.   while (string && whitespace (*string))
  5851.     string++;
  5852.  
  5853.   if (!string || !*string || *string == '#')
  5854.     return;
  5855.  
  5856.   /* If this is a parser directive, act on it. */
  5857.   if (*string == '$')
  5858.     {
  5859.       handle_parser_directive (&string[1]);
  5860.       return;
  5861.     }
  5862.  
  5863.   /* If we are supposed to be skipping parsing right now, then do it. */
  5864.   if (parsing_conditionalized_out)
  5865.     return;
  5866.  
  5867.   i = 0;
  5868.   /* If this keyname is a complex key expression surrounded by quotes,
  5869.      advance to after the matching close quote. */
  5870.   if (*string == '"')
  5871.     {
  5872.       for (i = 1; c = string[i]; i++)
  5873.     {
  5874.       if (c == '"' && string[i - 1] != '\\')
  5875.         break;
  5876.     }
  5877.     }
  5878.  
  5879.   /* Advance to the colon (:) or whitespace which separates the two objects. */
  5880.   for (; (c = string[i]) && c != ':' && c != ' ' && c != '\t'; i++ );
  5881.  
  5882.   /* Mark the end of the command (or keyname). */
  5883.   if (string[i])
  5884.     string[i++] = '\0';
  5885.  
  5886.   /* If this is a command to set a variable, then do that. */
  5887.   if (stricmp (string, "set") == 0)
  5888.     {
  5889.       char *var = string + i;
  5890.       char *value;
  5891.  
  5892.       /* Make VAR point to start of variable name. */
  5893.       while (*var && whitespace (*var)) var++;
  5894.  
  5895.       /* Make value point to start of value string. */
  5896.       value = var;
  5897.       while (*value && !whitespace (*value)) value++;
  5898.       if (*value)
  5899.     *value++ = '\0';
  5900.       while (*value && whitespace (*value)) value++;
  5901.  
  5902.       rl_variable_bind (var, value);
  5903.       return;
  5904.     }
  5905.  
  5906.   /* Skip any whitespace between keyname and funname. */
  5907.   for (; string[i] && whitespace (string[i]); i++);
  5908.   funname = &string[i];
  5909.  
  5910.   /* Now isolate funname.
  5911.      For straight function names just look for whitespace, since
  5912.      that will signify the end of the string.  But this could be a
  5913.      macro definition.  In that case, the string is quoted, so skip
  5914.      to the matching delimiter. */
  5915.   if (*funname == '\'' || *funname == '"')
  5916.     {
  5917.       int delimiter = string[i++];
  5918.  
  5919.       for (; c = string[i]; i++)
  5920.     {
  5921.       if (c == delimiter && string[i - 1] != '\\')
  5922.         break;
  5923.     }
  5924.       if (c)
  5925.     i++;
  5926.     }
  5927.  
  5928.   /* Advance to the end of the string.  */
  5929.   for (; string[i] && !whitespace (string[i]); i++);
  5930.  
  5931.   /* No extra whitespace at the end of the string. */
  5932.   string[i] = '\0';
  5933.  
  5934.   /* If this is a new-style key-binding, then do the binding with
  5935.      rl_set_key ().  Otherwise, let the older code deal with it. */
  5936.   if (*string == '"')
  5937.     {
  5938.       char *seq = (char *)alloca (1 + strlen (string));
  5939.       register int j, k = 0;
  5940.  
  5941.       for (j = 1; string[j]; j++)
  5942.     {
  5943.       if (string[j] == '"' && string[j - 1] != '\\')
  5944.         break;
  5945.  
  5946.       seq[k++] = string[j];
  5947.     }
  5948.       seq[k] = '\0';
  5949.  
  5950.       /* Binding macro? */
  5951.       if (*funname == '\'' || *funname == '"')
  5952.     {
  5953.       j = strlen (funname);
  5954.  
  5955.       if (j && funname[j - 1] == *funname)
  5956.         funname[j - 1] = '\0';
  5957.  
  5958.       rl_macro_bind (seq, &funname[1], keymap);
  5959.     }
  5960.       else
  5961.     rl_set_key (seq, rl_named_function (funname), keymap);
  5962.  
  5963.       return;
  5964.     }
  5965.  
  5966.   /* Get the actual character we want to deal with. */
  5967.   kname = rindex (string, '-');
  5968.   if (!kname)
  5969.     kname = string;
  5970.   else
  5971.     kname++;
  5972.  
  5973.   key = glean_key_from_name (kname);
  5974.  
  5975.   /* Add in control and meta bits. */
  5976.   if (substring_member_of_array (string, possible_control_prefixes))
  5977.     key = CTRL (to_upper (key));
  5978.  
  5979.   if (substring_member_of_array (string, possible_meta_prefixes))
  5980.     key = META (key);
  5981.  
  5982.   /* Temporary.  Handle old-style keyname with macro-binding. */
  5983.   if (*funname == '\'' || *funname == '"')
  5984.     {
  5985.       char seq[2];
  5986.       int fl = strlen (funname);
  5987.  
  5988.       seq[0] = key; seq[1] = '\0';
  5989.       if (fl && funname[fl - 1] == *funname)
  5990.     funname[fl - 1] = '\0';
  5991.  
  5992.       rl_macro_bind (seq, &funname[1], keymap);
  5993.     }
  5994. #if defined (PREFIX_META_HACK)
  5995.   /* Ugly, but working hack to keep prefix-meta around. */
  5996.   else if (stricmp (funname, "prefix-meta") == 0)
  5997.     {
  5998.       char seq[2];
  5999.  
  6000.       seq[0] = key;
  6001.       seq[1] = '\0';
  6002.       rl_generic_bind (ISKMAP, seq, (char *)emacs_meta_keymap, keymap);
  6003.     }
  6004. #endif /* PREFIX_META_HACK */
  6005.   else
  6006.     rl_bind_key (key, rl_named_function (funname));
  6007. }
  6008.  
  6009. rl_variable_bind (name, value)
  6010.      char *name, *value;
  6011. {
  6012.   if (stricmp (name, "editing-mode") == 0)
  6013.     {
  6014.       if (strnicmp (value, "vi", 2) == 0)
  6015.     {
  6016. #if defined (VI_MODE)
  6017.       keymap = vi_insertion_keymap;
  6018.       rl_editing_mode = vi_mode;
  6019. #else
  6020. #if defined (NOTDEF)
  6021.       /* What state is the terminal in?  I'll tell you:
  6022.          non-determinate!  That means we cannot do any output. */
  6023.       ding ();
  6024. #endif /* NOTDEF */
  6025. #endif /* VI_MODE */
  6026.     }
  6027.       else if (strnicmp (value, "emacs", 5) == 0)
  6028.     {
  6029.       keymap = emacs_standard_keymap;
  6030.       rl_editing_mode = emacs_mode;
  6031.     }
  6032.     }
  6033.   else if (stricmp (name, "horizontal-scroll-mode") == 0)
  6034.     {
  6035.       if (!*value || stricmp (value, "On") == 0)
  6036.     horizontal_scroll_mode = 1;
  6037.       else
  6038.     horizontal_scroll_mode = 0;
  6039.     }
  6040.   else if (stricmp (name, "mark-modified-lines") == 0)
  6041.     {
  6042.       if (!*value || stricmp (value, "On") == 0)
  6043.     mark_modified_lines = 1;
  6044.       else
  6045.     mark_modified_lines = 0;
  6046.     }
  6047.   else if (stricmp (name, "prefer-visible-bell") == 0)
  6048.     {
  6049.       if (!*value || stricmp (value, "On") == 0)
  6050.         prefer_visible_bell = 1;
  6051.       else
  6052.         prefer_visible_bell = 0;
  6053.     }
  6054.   else if (stricmp (name, "comment-begin") == 0)
  6055.     {
  6056. #if defined (VI_MODE)
  6057.       extern char *rl_vi_comment_begin;
  6058.  
  6059.       if (*value)
  6060.     {
  6061.       if (rl_vi_comment_begin)
  6062.         free (rl_vi_comment_begin);
  6063.  
  6064.       rl_vi_comment_begin = savestring (value);
  6065.     }
  6066. #endif /* VI_MODE */
  6067.     }
  6068. }
  6069.  
  6070. /* Return the character which matches NAME.
  6071.    For example, `Space' returns ' '. */
  6072.  
  6073. typedef struct {
  6074.   char *name;
  6075.   int value;
  6076. } assoc_list;
  6077.  
  6078. assoc_list name_key_alist[] = {
  6079.   { "DEL", 0x7f },
  6080.   { "ESC", '\033' },
  6081.   { "Escape", '\033' },
  6082.   { "LFD", '\n' },
  6083.   { "Newline", '\n' },
  6084.   { "RET", '\r' },
  6085.   { "Return", '\r' },
  6086.   { "Rubout", 0x7f },
  6087.   { "SPC", ' ' },
  6088.   { "Space", ' ' },
  6089.   { "Tab", 0x09 },
  6090.   { (char *)0x0, 0 }
  6091. };
  6092.  
  6093. int
  6094. glean_key_from_name (name)
  6095.      char *name;
  6096. {
  6097.   register int i;
  6098.  
  6099.   for (i = 0; name_key_alist[i].name; i++)
  6100.     if (stricmp (name, name_key_alist[i].name) == 0)
  6101.       return (name_key_alist[i].value);
  6102.  
  6103.   return (*name);
  6104. }
  6105.  
  6106.  
  6107. /* **************************************************************** */
  6108. /*                                    */
  6109. /*          Key Binding and Function Information            */
  6110. /*                                    */
  6111. /* **************************************************************** */
  6112.  
  6113. /* Each of the following functions produces information about the
  6114.    state of keybindings and functions known to Readline.  The info
  6115.    is always printed to rl_outstream, and in such a way that it can
  6116.    be read back in (i.e., passed to rl_parse_and_bind (). */
  6117.  
  6118. /* Print the names of functions known to Readline. */
  6119. void
  6120. rl_list_funmap_names (ignore)
  6121.      int ignore;
  6122. {
  6123.   register int i;
  6124.   char **funmap_names;
  6125.   extern char **rl_funmap_names ();
  6126.  
  6127.   funmap_names = rl_funmap_names ();
  6128.  
  6129.   if (!funmap_names)
  6130.     return;
  6131.  
  6132.   for (i = 0; funmap_names[i]; i++)
  6133.     fprintf (rl_outstream, "%s\n", funmap_names[i]);
  6134.  
  6135.   free (funmap_names);
  6136. }
  6137.  
  6138. /* Return a NULL terminated array of strings which represent the key
  6139.    sequences that are used to invoke FUNCTION in MAP. */
  6140. static char **
  6141. invoking_keyseqs_in_map (function, map)
  6142.      Function *function;
  6143.      Keymap map;
  6144. {
  6145.   register int key;
  6146.   char **result;
  6147.   int result_index, result_size;
  6148.  
  6149.   result = (char **)NULL;
  6150.   result_index = result_size = 0;
  6151.  
  6152.   for (key = 0; key < 128; key++)
  6153.     {
  6154.       switch (map[key].type)
  6155.     {
  6156.     case ISMACR:
  6157.       /* Macros match, if, and only if, the pointers are identical.
  6158.          Thus, they are treated exactly like functions in here. */
  6159.     case ISFUNC:
  6160.       /* If the function in the keymap is the one we are looking for,
  6161.          then add the current KEY to the list of invoking keys. */
  6162.       if (map[key].function == function)
  6163.         {
  6164.           char *keyname = (char *)xmalloc (5);
  6165.  
  6166.           if (CTRL_P (key))
  6167.         sprintf (keyname, "\\C-%c", to_lower (UNCTRL (key)));
  6168.           else if (key == RUBOUT)
  6169.         sprintf (keyname, "\\C-?");
  6170.           else
  6171.         sprintf (keyname, "%c", key);
  6172.           
  6173.           if (result_index + 2 > result_size)
  6174.         {
  6175.           if (!result)
  6176.             result = (char **) xmalloc
  6177.               ((result_size = 10) * sizeof (char *));
  6178.           else
  6179.             result = (char **) xrealloc
  6180.               (result, (result_size += 10) * sizeof (char *));
  6181.         }
  6182.  
  6183.           result[result_index++] = keyname;
  6184.           result[result_index] = (char *)NULL;
  6185.         }
  6186.       break;
  6187.  
  6188.     case ISKMAP:
  6189.       {
  6190.         char **seqs = (char **)NULL;
  6191.  
  6192.         /* Find the list of keyseqs in this map which have FUNCTION as
  6193.            their target.  Add the key sequences found to RESULT. */
  6194.         if (map[key].function)
  6195.           seqs =
  6196.         invoking_keyseqs_in_map (function, (Keymap)map[key].function);
  6197.  
  6198.         if (seqs)
  6199.           {
  6200.         register int i;
  6201.  
  6202.         for (i = 0; seqs[i]; i++)
  6203.           {
  6204.             char *keyname = (char *)xmalloc (6 + strlen (seqs[i]));
  6205.  
  6206.             if (key == ESC)
  6207.               sprintf (keyname, "\\e");
  6208.             else if (CTRL_P (key))
  6209.               sprintf (keyname, "\\C-%c", to_lower (UNCTRL (key)));
  6210.             else if (key == RUBOUT)
  6211.               sprintf (keyname, "\\C-?");
  6212.             else
  6213.               sprintf (keyname, "%c", key);
  6214.  
  6215.             strcat (keyname, seqs[i]);
  6216.  
  6217.             if (result_index + 2 > result_size)
  6218.               {
  6219.             if (!result)
  6220.               result = (char **)
  6221.                 xmalloc ((result_size = 10) * sizeof (char *));
  6222.             else
  6223.               result = (char **)
  6224.                 xrealloc (result,
  6225.                       (result_size += 10) * sizeof (char *));
  6226.               }
  6227.  
  6228.             result[result_index++] = keyname;
  6229.             result[result_index] = (char *)NULL;
  6230.           }
  6231.           }
  6232.       }
  6233.       break;
  6234.     }
  6235.     }
  6236.   return (result);
  6237. }
  6238.  
  6239. /* Return a NULL terminated array of strings which represent the key
  6240.    sequences that can be used to invoke FUNCTION using the current keymap. */
  6241. char **
  6242. rl_invoking_keyseqs (function)
  6243.      Function *function;
  6244. {
  6245.   return (invoking_keyseqs_in_map (function, keymap));
  6246. }
  6247.  
  6248. /* Print all of the current functions and their bindings to
  6249.    rl_outstream.  If an explicit argument is given, then print
  6250.    the output in such a way that it can be read back in. */
  6251. int
  6252. rl_dump_functions (count)
  6253.      int count;
  6254. {
  6255.   void rl_function_dumper ();
  6256.  
  6257.   rl_function_dumper (rl_explicit_arg);
  6258.   rl_on_new_line ();
  6259.   return (0);
  6260. }
  6261.  
  6262. /* Print all of the functions and their bindings to rl_outstream.  If
  6263.    PRINT_READABLY is non-zero, then print the output in such a way
  6264.    that it can be read back in. */
  6265. void
  6266. rl_function_dumper (print_readably)
  6267.      int print_readably;
  6268. {
  6269.   register int i;
  6270.   char **rl_funmap_names (), **names;
  6271.   char *name;
  6272.  
  6273.   names = rl_funmap_names ();
  6274.  
  6275.   fprintf (rl_outstream, "\n");
  6276.  
  6277.   for (i = 0; name = names[i]; i++)
  6278.     {
  6279.       Function *function;
  6280.       char **invokers;
  6281.  
  6282.       function = rl_named_function (name);
  6283.       invokers = invoking_keyseqs_in_map (function, keymap);
  6284.  
  6285.       if (print_readably)
  6286.     {
  6287.       if (!invokers)
  6288.         fprintf (rl_outstream, "# %s (not bound)\n", name);
  6289.       else
  6290.         {
  6291.           register int j;
  6292.  
  6293.           for (j = 0; invokers[j]; j++)
  6294.         {
  6295.           fprintf (rl_outstream, "\"%s\": %s\n",
  6296.                invokers[j], name);
  6297.           free (invokers[j]);
  6298.         }
  6299.  
  6300.           free (invokers);
  6301.         }
  6302.     }
  6303.       else
  6304.     {
  6305.       if (!invokers)
  6306.         fprintf (rl_outstream, "%s is not bound to any keys\n",
  6307.              name);
  6308.       else
  6309.         {
  6310.           register int j;
  6311.  
  6312.           fprintf (rl_outstream, "%s can be found on ", name);
  6313.  
  6314.           for (j = 0; invokers[j] && j < 5; j++)
  6315.         {
  6316.           fprintf (rl_outstream, "\"%s\"%s", invokers[j],
  6317.                invokers[j + 1] ? ", " : ".\n");
  6318.         }
  6319.  
  6320.           if (j == 5 && invokers[j])
  6321.         fprintf (rl_outstream, "...\n");
  6322.  
  6323.           for (j = 0; invokers[j]; j++)
  6324.         free (invokers[j]);
  6325.  
  6326.           free (invokers);
  6327.         }
  6328.     }
  6329.     }
  6330. }
  6331.  
  6332.  
  6333. /* **************************************************************** */
  6334. /*                                    */
  6335. /*            String Utility Functions            */
  6336. /*                                    */
  6337. /* **************************************************************** */
  6338.  
  6339. static char *strindex ();
  6340.  
  6341. /* Return non-zero if any members of ARRAY are a substring in STRING. */
  6342. static int
  6343. substring_member_of_array (string, array)
  6344.      char *string, **array;
  6345. {
  6346.   while (*array)
  6347.     {
  6348.       if (strindex (string, *array))
  6349.     return (1);
  6350.       array++;
  6351.     }
  6352.   return (0);
  6353. }
  6354.  
  6355. /* Whoops, Unix doesn't have strnicmp. */
  6356.  
  6357. /* Compare at most COUNT characters from string1 to string2.  Case
  6358.    doesn't matter. */
  6359. static int
  6360. strnicmp (string1, string2, count)
  6361.      char *string1, *string2;
  6362. {
  6363.   register char ch1, ch2;
  6364.  
  6365.   while (count)
  6366.     {
  6367.       ch1 = *string1++;
  6368.       ch2 = *string2++;
  6369.       if (to_upper(ch1) == to_upper(ch2))
  6370.     count--;
  6371.       else break;
  6372.     }
  6373.   return (count);
  6374. }
  6375.  
  6376. /* strcmp (), but caseless. */
  6377. static int
  6378. stricmp (string1, string2)
  6379.      char *string1, *string2;
  6380. {
  6381.   register char ch1, ch2;
  6382.  
  6383.   while (*string1 && *string2)
  6384.     {
  6385.       ch1 = *string1++;
  6386.       ch2 = *string2++;
  6387.       if (to_upper(ch1) != to_upper(ch2))
  6388.     return (1);
  6389.     }
  6390.   return (*string1 | *string2);
  6391. }
  6392.  
  6393. /* Determine if s2 occurs in s1.  If so, return a pointer to the
  6394.    match in s1.  The compare is case insensitive. */
  6395. static char *
  6396. strindex (s1, s2)
  6397.      register char *s1, *s2;
  6398. {
  6399.   register int i, l = strlen (s2);
  6400.   register int len = strlen (s1);
  6401.  
  6402.   for (i = 0; (len - i) >= l; i++)
  6403.     if (strnicmp (&s1[i], s2, l) == 0)
  6404.       return (s1 + i);
  6405.   return ((char *)NULL);
  6406. }
  6407.  
  6408.  
  6409. /* **************************************************************** */
  6410. /*                                    */
  6411. /*            USG (System V) Support                */
  6412. /*                                    */
  6413. /* **************************************************************** */
  6414.  
  6415. /* When compiling and running in the `Posix' environment, Ultrix does
  6416.    not restart system calls, so this needs to do it. */
  6417. int
  6418. rl_getc (stream)
  6419.      FILE *stream;
  6420. {
  6421.   int result;
  6422.   unsigned char c;
  6423.  
  6424. #ifdef __GO32__
  6425.   if (isatty(0))
  6426.     return getkey();
  6427. #endif /* __GO32__ */
  6428.  
  6429.   while (1)
  6430.     {
  6431.       result = read (fileno (stream), &c, sizeof (char));
  6432.  
  6433.       if (result == sizeof (char))
  6434.     return (c);
  6435.  
  6436.       /* If zero characters are returned, then the file that we are
  6437.      reading from is empty!  Return EOF in that case. */
  6438.       if (result == 0)
  6439.     return (EOF);
  6440.  
  6441. #ifndef __GO32__
  6442.       /* If the error that we received was SIGINT, then try again,
  6443.      this is simply an interrupted system call to read ().
  6444.      Otherwise, some error ocurred, also signifying EOF. */
  6445.       if (errno != EINTR)
  6446.     return (EOF);
  6447. #endif /* !__GO32__ */
  6448.     }
  6449. }
  6450.  
  6451. #if defined (STATIC_MALLOC)
  6452.  
  6453. /* **************************************************************** */
  6454. /*                                    */
  6455. /*            xmalloc and xrealloc ()                     */
  6456. /*                                    */
  6457. /* **************************************************************** */
  6458.  
  6459. static void memory_error_and_abort ();
  6460.  
  6461. static char *
  6462. xmalloc (bytes)
  6463.      int bytes;
  6464. {
  6465.   char *temp = (char *)malloc (bytes);
  6466.  
  6467.   if (!temp)
  6468.     memory_error_and_abort ();
  6469.   return (temp);
  6470. }
  6471.  
  6472. static char *
  6473. xrealloc (pointer, bytes)
  6474.      char *pointer;
  6475.      int bytes;
  6476. {
  6477.   char *temp;
  6478.  
  6479.   if (!pointer)
  6480.     temp = (char *)malloc (bytes);
  6481.   else
  6482.     temp = (char *)realloc (pointer, bytes);
  6483.  
  6484.   if (!temp)
  6485.     memory_error_and_abort ();
  6486.  
  6487.   return (temp);
  6488. }
  6489.  
  6490. static void
  6491. memory_error_and_abort ()
  6492. {
  6493.   fprintf (stderr, "readline: Out of virtual memory!\n");
  6494.   abort ();
  6495. }
  6496. #endif /* STATIC_MALLOC */
  6497.  
  6498.  
  6499. /* **************************************************************** */
  6500. /*                                    */
  6501. /*            Testing Readline                */
  6502. /*                                    */
  6503. /* **************************************************************** */
  6504.  
  6505. #if defined (TEST)
  6506.  
  6507. main ()
  6508. {
  6509.   HIST_ENTRY **history_list ();
  6510.   char *temp = (char *)NULL;
  6511.   char *prompt = "readline% ";
  6512.   int done = 0;
  6513.  
  6514.   while (!done)
  6515.     {
  6516.       temp = readline (prompt);
  6517.  
  6518.       /* Test for EOF. */
  6519.       if (!temp)
  6520.     exit (1);
  6521.  
  6522.       /* If there is anything on the line, print it and remember it. */
  6523.       if (*temp)
  6524.     {
  6525.       fprintf (stderr, "%s\r\n", temp);
  6526.       add_history (temp);
  6527.     }
  6528.  
  6529.       /* Check for `command' that we handle. */
  6530.       if (strcmp (temp, "quit") == 0)
  6531.     done = 1;
  6532.  
  6533.       if (strcmp (temp, "list") == 0)
  6534.     {
  6535.       HIST_ENTRY **list = history_list ();
  6536.       register int i;
  6537.       if (list)
  6538.         {
  6539.           for (i = 0; list[i]; i++)
  6540.         {
  6541.           fprintf (stderr, "%d: %s\r\n", i, list[i]->line);
  6542.           free (list[i]->line);
  6543.         }
  6544.           free (list);
  6545.         }
  6546.     }
  6547.       free (temp);
  6548.     }
  6549. }
  6550.  
  6551. #endif /* TEST */
  6552.  
  6553.  
  6554. /*
  6555.  * Local variables:
  6556.  * compile-command: "gcc -g -traditional -I. -I.. -DTEST -o readline readline.c keymaps.o funmap.o history.o -ltermcap"
  6557.  * end:
  6558.  */
  6559. @
  6560.